添加程序结束判断,所有activity和service都结束

pull/1114/head
gedoor 3 years ago
parent c5f9c85a70
commit 15d70507c3
  1. 15
      app/src/main/java/io/legado/app/help/LifecycleHelp.kt

@ -16,6 +16,7 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
private val activities: MutableList<WeakReference<Activity>> = arrayListOf() private val activities: MutableList<WeakReference<Activity>> = arrayListOf()
private val services: MutableList<WeakReference<BaseService>> = arrayListOf() private val services: MutableList<WeakReference<BaseService>> = arrayListOf()
private var finishedListener: (() -> Unit)? = null
fun activitySize(): Int { fun activitySize(): Int {
return activities.size return activities.size
@ -51,6 +52,10 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
} }
} }
fun setOnFinishedListener(finishedListener: (() -> Unit)) {
this.finishedListener = finishedListener
}
override fun onActivityPaused(activity: Activity) { override fun onActivityPaused(activity: Activity) {
} }
@ -65,6 +70,9 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
for (temp in activities) { for (temp in activities) {
if (temp.get() != null && temp.get() === activity) { if (temp.get() != null && temp.get() === activity) {
activities.remove(temp) activities.remove(temp)
if (services.size == 0 && activities.size == 0) {
onFinished()
}
break break
} }
} }
@ -93,8 +101,15 @@ object LifecycleHelp : Application.ActivityLifecycleCallbacks {
for (temp in services) { for (temp in services) {
if (temp.get() != null && temp.get() === service) { if (temp.get() != null && temp.get() === service) {
services.remove(temp) services.remove(temp)
if (services.size == 0 && activities.size == 0) {
onFinished()
}
break break
} }
} }
} }
private fun onFinished() {
finishedListener?.invoke()
}
} }
Loading…
Cancel
Save