main

square/leakcanary

Last updated at: 29/12/2023 09:38

ActivityWatcher.kt

TLDR

The ActivityWatcher class is a component of the LeakCanary library that expects activities to become weakly reachable soon after they receive the Activity.onDestroy callback. It is used to monitor activities for potential leaks.

Classes

ActivityWatcher

The ActivityWatcher class is responsible for monitoring activities and expecting them to become weakly reachable after receiving the Activity.onDestroy callback. It implements the InstallableWatcher interface and provides methods for installation and uninstallation.

Methods

This file does not contain any additional methods.

package leakcanary

import android.app.Activity
import android.app.Application
import leakcanary.internal.friendly.noOpDelegate

/**
 * Expects activities to become weakly reachable soon after they receive the [Activity.onDestroy]
 * callback.
 */
class ActivityWatcher(
  private val application: Application,
  private val reachabilityWatcher: ReachabilityWatcher
) : InstallableWatcher {

  private val lifecycleCallbacks =
    object : Application.ActivityLifecycleCallbacks by noOpDelegate() {
      override fun onActivityDestroyed(activity: Activity) {
        reachabilityWatcher.expectWeaklyReachable(
          activity, "${activity::class.java.name} received Activity#onDestroy() callback"
        )
      }
    }

  override fun install() {
    application.registerActivityLifecycleCallbacks(lifecycleCallbacks)
  }

  override fun uninstall() {
    application.unregisterActivityLifecycleCallbacks(lifecycleCallbacks)
  }
}