From de307d2d6f662e68f9eb0a48b56fae7afe8c93b6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 7 Mar 2020 22:36:40 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/App.kt | 36 ++----------------- .../java/io/legado/app/help/ActivityHelp.kt | 27 +++++++++++++- 2 files changed, 28 insertions(+), 35 deletions(-) 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