如何查询堆栈中的活动? [英] How do I query the activities on the stack?
问题描述
我有一个编辑联系人项目的活动.除了可以在应用程序内通过导航调用之外,我还有一个主屏幕快捷键,可用于EditItem活动.
I have an activity that edits a contact item. As well as being invoked by navigation within the app, I have a home screen shortcut to the EditItem activity.
我的问题是,潜在的用户可能正在编辑联系人,忘记了他正在这样做,然后通过快捷方式重新编辑了该联系人,即,堆栈上可能有两个EditItem活动.如果发生这种情况,则其中一个进行的编辑会破坏另一个.
My problem is that potentially a user could be editing a contact, forget that he was doing so, and re-edit the contact via the shortcut, ie there could be two EditItem activities on the stack. If this happens, edits from one will clobber the other.
我如何检测和/或避免这种情况?
How do I detect and/or avoid this scenario?
推荐答案
只需在 AndroidManifest.xml
文件中将您的编辑活动标记为"singleTask":
Just mark your edit activity as a "singleTask" in your AndroidManifest.xml
file:
<activity
android:name=".EditActivity"
android:label="@string/app_name"
android:launchMode="singleTask" />
您可以在launchMode 模式下进行阅读="nofollow"> Android开发人员指南.要引用它,
You can read up on the launchMode
mode in the Android Developer Guide. To quote it,
系统在新任务的根部创建活动并进行路由目的.但是,如果该活动的实例已经存在存在,系统通过以下方式将意图路由到现有实例调用其onNewIntent()方法,而不是创建一个新方法.
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.
例如,我已经将此模式用于地图活动,这可能会导致活动堆栈中的循环.为了避免用户执行活动A-> B-> C-> B-> C,然后必须将所有活动都按Back,我将活动C设置为"singleTask".这样,用户只需在活动B中按一次返回"即可到达活动A.
For instance, I've used this mode for a map activity that could cause cycles in the activity stack. To avoid a user doing activity A -> B -> C -> B -> C and then having to press Back through all of them, I made activity C a "singleTask". That way the user only ever had to press Back once from activity B to reach activity A.
这篇关于如何查询堆栈中的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!