From 9ab7550970d0bb1222ec721cca534c1dac7cfb43 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Thu, 2 Jun 2022 02:18:11 -0700 Subject: [PATCH] Use weak reference to track activity --- app/src/main/java/com/topjohnwu/magisk/core/App.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/core/App.kt b/app/src/main/java/com/topjohnwu/magisk/core/App.kt index d764281cf..1c300b7c5 100644 --- a/app/src/main/java/com/topjohnwu/magisk/core/App.kt +++ b/app/src/main/java/com/topjohnwu/magisk/core/App.kt @@ -1,6 +1,5 @@ package com.topjohnwu.magisk.core -import android.annotation.SuppressLint import android.app.Activity import android.app.Application import android.content.Context @@ -15,6 +14,7 @@ import com.topjohnwu.superuser.internal.UiThreadHandler import com.topjohnwu.superuser.ipc.RootService import kotlinx.coroutines.Dispatchers import timber.log.Timber +import java.lang.ref.WeakReference import kotlin.system.exitProcess open class App() : Application() { @@ -81,20 +81,21 @@ open class App() : Application() { } } -@SuppressLint("StaticFieldLeak") object ActivityTracker : Application.ActivityLifecycleCallbacks { + val foreground: Activity? get() = ref.get() + @Volatile - var foreground: Activity? = null + private var ref = WeakReference(null) override fun onActivityResumed(activity: Activity) { if (activity is SuRequestActivity) return - foreground = activity + ref = WeakReference(activity) } override fun onActivityPaused(activity: Activity) { if (activity is SuRequestActivity) return - foreground = null + ref.clear() } override fun onActivityCreated(activity: Activity, bundle: Bundle?) {}