在Android设计中遇到这样一个问题:
Activity A,在AndroidManifest.xml中设置它的一个<inter-filter>为
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
A的launchMode为“singleTask”。
Activity B,设置launchMode为“standard”。
这样,A作为启动应用的入口Activity;在A中进行某个操作可以跳转到B。
如果此时,点击‘Home’键返回到Home Screen后,再次点击应用图标进入应用,出现的界面为A,而非B。通过一些测试,发现,在重新进入应用时,在Back stack中,位于A之上的其他Activity均被销毁了。
我希望改变这种用户体验,让用户在重新进入时可以看到B,如何做呢?
为了实现这个目标,先了解了Android中和Activity,Task,Back stack有关的内容。下面先列出对解决问题有帮助的资料:
1.区分Activity的四种加载模式
这个web,简明地用示例对Activity的四种launchMode做了说明。在没有阅读官方文档前,对launchMode的入门理解很有帮助。而对Activity,Task,Back stack有了一定认识后,再次阅读,也看出了之前没有理解的东西。
2.官方文档
位于docs/guide/topics/manifest/activity-element.html#lmode下的内容,解释对AndroidManifest.xml中设置activity的launchMode设置。
位于docs/guide/topics/fundamentals/tasks-and-back-stack.html下的内容,解释“Tasks and Back Stack“。
3.Android Activities and Tasks
series – An introduction to Android’s UI component model(http://blog.akquinet.de/2010/02/17/android-activites-and-tasks-series-an-introduction-to-androids-ui-component-model/)
这个blog比较完整地和Activity,task有关的内容。下面贴出它的前言部分:
With Android, Google implemented its very own UI component model,
introducing various new terms and concepts probably unfamiliar to most
developers. Although well designed and powerful, it is unconventional,
has a high learning curve and, in some respects, lacks sufficient
documentation. Basic use cases may be quick to implement, but in order
to develop more complex applications, a thorough understanding of
Android’s UI component model is essential.
This series aims at providing this understanding while focusing on the UI related concepts of
activities and
tasks.
Overview
This series is composed out of four posts:
-
This first post gives an introduction to Android’s UI component
model and briefly explains the concepts behind activities and tasks.
-
The second post details the various
intent flags used to customize the start of activies and influence corresponding behavior (
caller).
-
The third post deals with the different activity properties specifyable for the called activity (
callee).
-
The fourth post concludes the series with an exploration of the different combinations of intent flags and activity properties.
另外,这个blog中还提供了一个应用,用以帮助理解Intent flag。
4.解开Android应用程序组件Activity的"singleTask"之谜
此blog的作者描述了他通过源码研究过程,主要讨论的是当Activity的launchMode为singleTask时,分配Task的问题。作者的结论如下:
至此,我们总结一下,设置了"singleTask"启动模式的Activity的特点:
1.
设置了"singleTask"启动模式的Activity,它在启动的时候,会先在系统中查找属性值affinity等于它的属性值
taskAffinity的任务存在;如果存在这样的任务,它就会在这个任务中启动,否则就会在新任务中启动。因此,如果我们想要设置
了"singleTask"启动模式的Activity在新的任务中启动,就要为它设置一个独立的taskAffinity属性值。
2.
如果设置了"singleTask"启动模式的Activity不是在新的任务中启动时,它会在已有的任务中查看是否已经存在相应的Activity实
例,如果存在,就会把位于这个Activity实例上面的Activity全部结束掉,即最终这个Activity实例会位于任务的堆栈顶端中。
这个blog中,让我了解了‘adb shell dumpsys activity’的使用。这可以将系统中和activity有关的一些内容转储出来。
最后,下面这个web中,提问者的问题和我想解决的是一致的。
Android: bug in launchMode=“singleTask”? -> activity stack not preserved
回答者renchenyu的答复给出了解决的思路:
This is not a bug. When an existed
"singleTask" activity is launching, all other activities above it in the
stack will be destroyed.
When you press HOME and launch the activity again, ActivityManger
calls an intent{act=android.intent.action.MAIN
cat=[android.intent.category.LAUNCHER]
flat=FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_RESET_IF_NEEDED cmp=A}, so the
result is A > B > HOME > A.
It's different when A's launchMode is "Standard". The task which
contains A will come to the foreground and keep the state the same as
before.
You can create a "Standard" activity eg. C as the launcher and startActivity(A) in the onCreate method of C
OR
Just remove the launchMode="singleTask" and set
FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an
intent to A
分享到:
相关推荐
android singleTask http://u.download.csdn.net/upload?ref=toolbar
NULL 博文链接:https://uuubd.iteye.com/blog/1900472
Android的Activity的Launch模式之SingleTask案例,实际案例源码
SingleTask与SingleInstance的区别,看过文档后还是不太明白,于是写了一个测试程序,运行TaskA,页面显示taskid,通过Next调出TaskB的Activity,每个页面都会显示当前的taskid,且通过页面透明叠加,可以清楚看到...
本文继续介绍Activity的下一个启动模式:singleTask。 singleTask:当设置活动的启动模式为singleTask时,首先检查返回栈中是否存在当前活动,如果存在当前活动的实例,则直接使用当前实例,并把当前活动之上的所有...
来自我的文章http://www.jianshu.com/p/71d99b8bfd5d 中的apk,关于singleTask在Nubia手机无效的问题
http://blog.csdn.net/dinko321/article/details/8314974 的验证代码
通过webview的配合,来观察activity在singleTask启动模式下跳转页面的变化
当应用运行起来后就会开启一条线程,线程中会运行一个任务栈,当Activity实例创建后就会放入任务栈中。Activity启动模式的设置在AndroidManifest.xml文件中,通过配置Activity的属性android:launchMode=""设置
此源代码为android播放器源码,运用到服务services,还android的任务singleTask并且解决其传递参数问题
Activity7大生命周期与4种启动模式(standard,singleTop,singleTask,singleIntance)
写了一篇关于Activity启动模式的文章,这是示例代码
在Android中每个界面都是一个Activity,切换界面操作其实是多个不同Activity之间的实例化操作。在Android中Activity的启动模式决定了Activity的启动运行方式。 Activity有四种启动模式: 1. standard,默认的启动...
然而,BUG是当恢复一个activity时,fragment transaction被执行了,应用就可能发生下面的下崩溃: 原因:不管何时,如果一个FragmentActivity放在后台,对应FragmentMangerI mpl中mStateSaved的flag就会设置为true...
一旦改模式的Activity的实例存在于某个栈中,任何应用再激活改Activity时都会重用该栈中的实例,其效果相当于多个应用程序共享一个应用,不管谁激活该Activity都会进入同一个应用中。 其中standard是系统默认的启动...
2 设置入口activity的启动模式android:launchMode="singleTask",重写onNewIntent方法, 在退出activity时,设置intent.setAction("exit_app");然后跳转到栈底activity, 这样就可以清除栈底之外的所有activity,...
Android 有四种启动模式, 分别是 standard、singleTop、singleTask 、singleInstance 这个Demo实现了其中的三个启动模式
这里展示了不同应用使用标记SingleTask,以及SingleInstance的task的情况,也只展示了应用间启动和task进入后台的情况。