diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 019325cd0..6908c5715 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -10,9 +10,9 @@ import com.jeremyliao.liveeventbus.LiveEventBus import io.legado.app.constant.AppConst.channelIdDownload import io.legado.app.constant.AppConst.channelIdReadAloud import io.legado.app.constant.AppConst.channelIdWeb -import io.legado.app.help.ActivityHelp import io.legado.app.help.AppConfig import io.legado.app.help.CrashHandler +import io.legado.app.help.LifecycleHelp import io.legado.app.help.ThemeConfig.applyDayNight import io.legado.app.utils.LanguageUtils import io.legado.app.utils.defaultSharedPreferences @@ -28,7 +28,7 @@ class App : MultiDexApplication() { LiveEventBus.config() .lifecycleObserverAlwaysActive(true) .autoClear(false) - registerActivityLifecycleCallbacks(ActivityHelp) + registerActivityLifecycleCallbacks(LifecycleHelp) defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) } diff --git a/app/src/main/java/io/legado/app/help/ActivityHelp.kt b/app/src/main/java/io/legado/app/help/LifecycleHelp.kt similarity index 67% rename from app/src/main/java/io/legado/app/help/ActivityHelp.kt rename to app/src/main/java/io/legado/app/help/LifecycleHelp.kt index b5e1dbe28..bf03bd87c 100644 --- a/app/src/main/java/io/legado/app/help/ActivityHelp.kt +++ b/app/src/main/java/io/legado/app/help/LifecycleHelp.kt @@ -11,18 +11,18 @@ import java.util.* * Activity管理器,管理项目中Activity的状态 */ @Suppress("unused") -object ActivityHelp : Application.ActivityLifecycleCallbacks { +object LifecycleHelp : Application.ActivityLifecycleCallbacks { private val activities: MutableList> = arrayListOf() - fun size(): Int { + fun activitySize(): Int { return activities.size } /** * 判断指定Activity是否存在 */ - fun isExist(activityClass: Class<*>): Boolean { + fun isExistActivity(activityClass: Class<*>): Boolean { activities.forEach { item -> if (item.get()?.javaClass == activityClass) { return true @@ -31,47 +31,6 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { return false } - /** - * 添加Activity - */ - fun add(activity: Activity) { - activities.add(WeakReference(activity)) - } - - /** - * 移除Activity - */ - fun remove(activity: Activity) { - for (temp in activities) { - if (null != temp.get() && temp.get() === activity) { - activities.remove(temp) - break - } - } - } - - /** - * 移除Activity - */ - fun remove(activityClass: Class<*>) { - val iterator = activities.iterator() - while (iterator.hasNext()) { - val item = iterator.next() - if (item.get()?.javaClass == activityClass) { - iterator.remove() - } - } - } - - /** - * 关闭指定 activity - */ - fun finishActivity(vararg activities: Activity) { - activities.forEach { activity -> - activity.finish() - } - } - /** * 关闭指定 activity(class) */ @@ -101,7 +60,12 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { } override fun onActivityDestroyed(activity: Activity) { - remove(activity) + for (temp in activities) { + if (temp.get() === activity) { + activities.remove(temp) + break + } + } } override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) { @@ -111,7 +75,7 @@ object ActivityHelp : Application.ActivityLifecycleCallbacks { } override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) { - add(activity) + activities.add(WeakReference(activity)) if (!LanguageUtils.isSameWithSetting(activity)){ LanguageUtils.setConfiguration(activity) } diff --git a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt index c25ebf2f1..d32e4934c 100644 --- a/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt +++ b/app/src/main/java/io/legado/app/receiver/MediaButtonReceiver.kt @@ -6,8 +6,8 @@ import android.content.Intent import android.view.KeyEvent import io.legado.app.constant.EventBus import io.legado.app.data.appDb -import io.legado.app.help.ActivityHelp import io.legado.app.help.AppConfig +import io.legado.app.help.LifecycleHelp import io.legado.app.service.AudioPlayService import io.legado.app.service.BaseReadAloudService import io.legado.app.service.help.AudioPlay @@ -69,13 +69,13 @@ class MediaButtonReceiver : BroadcastReceiver() { } else { AudioPlay.pause(context) } - ActivityHelp.isExist(ReadBookActivity::class.java) -> + LifecycleHelp.isExistActivity(ReadBookActivity::class.java) -> postEvent(EventBus.MEDIA_BUTTON, true) - ActivityHelp.isExist(AudioPlayActivity::class.java) -> + LifecycleHelp.isExistActivity(AudioPlayActivity::class.java) -> postEvent(EventBus.MEDIA_BUTTON, true) else -> if (AppConfig.mediaButtonOnExit || !isMediaKey) { appDb.bookDao.lastReadBook?.let { - if (!ActivityHelp.isExist(MainActivity::class.java)) { + if (!LifecycleHelp.isExistActivity(MainActivity::class.java)) { Intent(context, MainActivity::class.java).let { it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) context.startActivity(it)