gpt4 book ai didi

android - 如何完成多个 SingleInstance Activity?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:44:28 26 4
gpt4 key购买 nike

我有几个使用 launchMode SingleInstance 的 Activity 。注销时我想完成所有 Activity 并打开 launchScreen。

val intent = Intent(context, LauncherActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
(context as AppCompatActivity).finishAffinity()
context.startActivity(intent)

但是,如果我在 Launcher Activity 中按回键,我将被转发到之前使用 singleInstance 模式启动的 Activity

最佳答案

2018 年 11 月 1 日更新:

我已经测试了多种处理它的方法,例如事件传播、 Intent 标志、计算 Activity 实例等。有一些奇怪的场景,例如顺序启动多个 singleInstance Activity 。在这种情况下,中间 Activity 根本不会启动(onCreate 方法未被调用)并且在按下后退按钮后,它们将启动。因此,之前的任何一种方法都行不通!由于这个问题有点奇怪,我尝试用一​​种有点奇怪的方式来解决它。

我们在名为 LogoutHandler 的单例对象中维护注销状态。它与 LogoutAwareActivity 类合作,该类被除 LoginActivity 之外的所有 Activity 继承,因为它不应受到注销机制的影响。当注销发生时,在 LogoutHandler 中设置一个标志,直到 LogoutAwareActivity 的最后一个子级完成然后清除该标志。

这是一个实现:

注销处理程序:

import java.util.*

object LogoutHandler {

private var isLogout = false
private var timerWatchDog: TimerWatchDog? = null

fun isLogout() = isLogout

fun onActivityDestroyed() {
if (isLogout) {
timerWatchDog?.refresh(Runnable {
isLogout = false
timerWatchDog = null
})
}
}

fun logout() {
isLogout = true
timerWatchDog = TimerWatchDog(500)
}

private class TimerWatchDog(private val delay: Long) : Runnable {

private var timer: Timer? = null
private var runnable: Runnable? = null

fun refresh(runnable: Runnable) {
this.runnable = runnable
timer?.cancel()

val timerTask = object : TimerTask() {
override fun run() {
Thread(this@TimerWatchDog).start()
}
}
timer = Timer()
timer?.schedule(timerTask, delay)
}

override fun run() {
runnable?.run()
}
}

}

LogoutAwareActivity:

import android.support.v7.app.AppCompatActivity

abstract class LogoutAwareActivity : AppCompatActivity() {

override fun onResume() {
super.onResume()
if (LogoutHandler.isLogout()) {
finish()
}
}

override fun onDestroy() {
super.onDestroy()
LoginHandler.onActivityDestroyed()
}

}

一个具体的 Activity :

class ActivityA : LogoutAwareActivity() {

// ...
}

另一个具体 Activity :

class ActivityB : LogoutAwareActivity() {

// ...
}

您的注销函数:

fun logout() {
val intent = Intent(context, LoginActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

LogoutHandler.logout()
context.startActivity(intent)
}

视觉结果:

MainActivityActivityAActivityBActivityC 都是单实例。

通过按返回按钮在 Activity 之间移动:

enter image description here

转到 LoginActivity 然后按返回按钮:

enter image description here

关于android - 如何完成多个 SingleInstance Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015187/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com