diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index d1a3e9464..7cb9eff1f 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -1,13 +1,11 @@ package io.legado.app -import android.app.Activity import android.app.Application import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context import android.content.res.Configuration import android.os.Build -import android.os.Bundle import androidx.annotation.RequiresApi import androidx.appcompat.app.AppCompatDelegate import com.jeremyliao.liveeventbus.LiveEventBus @@ -56,7 +54,7 @@ class App : Application() { .lifecycleObserverAlwaysActive(true) .autoClear(false) - registerActivityLife() + registerActivityLifecycleCallbacks(ActivityHelp) } override fun onConfigurationChanged(newConfig: Configuration) { @@ -116,9 +114,7 @@ class App : Application() { */ @RequiresApi(Build.VERSION_CODES.O) private fun createChannelId() { - val notificationManager = - getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager - notificationManager?.let { + (getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager)?.let { //用唯一的ID创建渠道对象 val downloadChannel = NotificationChannel( channelIdDownload, @@ -157,32 +153,4 @@ class App : Application() { } } - private fun registerActivityLife() { - registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks { - override fun onActivityPaused(activity: Activity) { - } - - override fun onActivityResumed(activity: Activity) { - } - - override fun onActivityStarted(activity: Activity) { - - } - - override fun onActivityDestroyed(activity: Activity) { - ActivityHelp.remove(activity) - } - - override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { - } - - override fun onActivityStopped(activity: Activity) { - } - - override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - ActivityHelp.add(activity) - } - - }) - } } diff --git a/app/src/main/java/io/legado/app/help/ActivityHelp.kt b/app/src/main/java/io/legado/app/help/ActivityHelp.kt index 6138c0c7e..b14c76670 100644 --- a/app/src/main/java/io/legado/app/help/ActivityHelp.kt +++ b/app/src/main/java/io/legado/app/help/ActivityHelp.kt @@ -1,13 +1,15 @@ package io.legado.app.help import android.app.Activity +import android.app.Application +import android.os.Bundle import java.lang.ref.WeakReference import java.util.* /** * Activity管理器,管理项目中Activity的状态 */ -object ActivityHelp { +object ActivityHelp : Application.ActivityLifecycleCallbacks { private val activities: MutableList> = arrayListOf() @@ -86,4 +88,27 @@ object ActivityHelp { } } + override fun onActivityPaused(activity: Activity) { + } + + override fun onActivityResumed(activity: Activity) { + } + + override fun onActivityStarted(activity: Activity) { + + } + + override fun onActivityDestroyed(activity: Activity) { + remove(activity) + } + + override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle?) { + } + + override fun onActivityStopped(activity: Activity) { + } + + override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { + add(activity) + } } \ No newline at end of file