- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有几个使用 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
最佳答案
我已经测试了多种处理它的方法,例如事件传播、 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)
}
视觉结果:
MainActivity
、ActivityA
、ActivityB
和 ActivityC
都是单实例。
通过按返回按钮在 Activity 之间移动:
转到 LoginActivity
然后按返回按钮:
关于android - 如何完成多个 SingleInstance Activity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53015187/
我想知道 singleInstance Activity 的默认任务关联是什么? 因为每个 singleInstance Activity 在新任务中作为根 Activity 打开。并且无需声明 an
我的应用程序中有 2 个 Activity 。 A Activity 是启动器 Activity 。当我第一次运行应用程序时,启动器 Activity 会运行,但是当我按下主页按钮并通过单击应用程序图
我试图更改 Activity 转换的默认动画,但我遇到了一个问题。 我可以更改正常 Activity 的动画,但是当我将启动模式更改为 singleInstance 时,我第一次打开 Activity
可能重复 Deep linking and multiple app instances .我在我的应用程序中实现了深度链接。我有作为启动器的 Splash Activity 和处理 list 中定义
据我所知,startActivityForResult 无法与 launchMode singleInstance 一起正常工作 here .而最近,我发现它在 Android Lollipop(5.
我有一个 Activity 在 list 中定义为: android:launchMode="singleInstance" 现在我想从一个不知道它存在(不引用现有实例)的不同 Activity 转
我有几个使用 launchMode SingleInstance 的 Activity 。注销时我想完成所有 Activity 并打开 launchScreen。 val intent = Inten
我有一个 Activity 显示列表中的多个元素,用户可以滚动浏览这些元素。除了浏览添加的新元素的列表之外,用户还可以单击弹出通知以立即转到该元素。我只希望运行列表的一个实例,所以我将其设为 Sing
一个 Activity 是一个单一的实例 Activity 。 import android.app.Activity; import android.content.Intent; import a
一个Activity有四种启动模式,standard、singletop、singletask和singleInstance。前三者比较容易理解,应用广泛。但是到现在为止,我没有发现任何使用singl
我想知道是否可以在启动 singleInstance Activity 时取消所有动画,我已经在用了 @Override public void onStop() { super.onStop
我已经阅读了谷歌 Android 开发者页面,但任务的概念 ( http://developer.android.com/guide/components/tasks-and-back-stack.h
我在我的应用程序中使用了 android:launchMode="singleInstance" 如何在 android api 2.3.3 中完美编译和运行但是当我在基于api 4.0 的andro
我遇到了一个有趣的问题,一个 Activity 被创建了多次,即使它在 list 中被定义为一个 singleTask 或一个 singelInstance Activity。这是如何重现的。比如说,
我在为不同的 Activity 实现不同类型的启动模式时遇到了设计问题。我有 5 个 Activity 。 视频列表 视频详情 收藏夹列表 视频搜索 视频播放器 当用户启动应用程序时,它会转到显示视频
我的应用程序包含多个 Activity ,主要 Activity (A) 处于 launchMode singleTop,每个 Activity 都处于 singleInstance 模式。 我选择了
我正在寻找何时使用 launchMode="singleInstance" 的具体示例。我到处阅读,据说很少有场合应该使用它。所以我很惊讶我从来没有看到例子! 我找到的最接近的解释是 here它说:
IProductRepositoryProxy ProductDataServiceProviderInstance = new ServiceProductDataProvider(); build
我在 StackOverflow 上没有找到任何可以回答我问题的帖子。我已经看过了Android singletop singleinstance and singletask但这个问题和他的项目场景
关闭。这个问题是opinion-based .它目前不接受答案。 关闭3年前. 锁定。这个问题及其答案是locked因为这个问题是题外话,但具有历史意义。它目前不接受新的答案或交互。 我有一个应用程序
我是一名优秀的程序员,十分优秀!