Android设计中和“singleTask“有关的一个设计问题
????? 在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.
This series is composed out of four posts:
????? 另外,这个blog中还提供了一个应用,用以帮助理解Intent flag。
????? 4.
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