java 使用 alwaysRetainTaskState 和 lauchMode 保留 android 应用程序状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2059344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retaining android app state using alwaysRetainTaskState and lauchMode
提问by jlim
In my Android app, I have a main activity that serves as an entry point to my application, which is configured in my manifest file like this :
在我的 Android 应用程序中,我有一个主要活动作为我的应用程序的入口点,它在我的清单文件中配置如下:
<activity android:name=".Main"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
So for a particular use case, lets say a user starts up the app from the home screen by clicking the icon inside the application launcher. After starting the app, the user navigates from the Main activity to activity A and then finally to activity B. At this point, the user decides to check their facebook, so they click the home button to put my app in the background, and launches the facebook app.
因此,对于特定用例,假设用户通过单击应用程序启动器内的图标从主屏幕启动应用程序。启动应用程序后,用户从主活动导航到活动A,最后导航到活动B。此时,用户决定查看他们的facebook,因此他们单击主页按钮将我的应用程序置于后台,然后启动脸书应用程序。
After checking their facebook, the user wants to return to my app, so they press the home key, and launch the application from the application launcher (just like they did the first time it was launched).
在查看他们的 facebook 后,用户想要返回我的应用程序,因此他们按主页键,然后从应用程序启动器启动应用程序(就像他们第一次启动时所做的那样)。
When a user returns to my app, I want the app to return to the last activity the user was at when the app was put into the background, which in this case is activity B. In the manifest file, I have set alwaysRetainTaskState=true to make sure the OS doesn't kill my app's activities.
当用户返回我的应用程序时,我希望应用程序返回到用户在将应用程序置于后台时所在的最后一个活动,在本例中为活动 B。在清单文件中,我设置了 alwaysRetainTaskState=true确保操作系统不会终止我的应用程序的活动。
Now to my question: how do I get the behavior I described above? Whenever I click my app's icon, it always starts at the Main activity, no matter what. I think this is because of the category.LAUNCHER attribute. I have tried android:launchMode=singleTask, but it hasn't made a difference; it always starts at Main.
现在我的问题是:我如何获得我上面描述的行为?每当我点击我的应用程序图标时,它总是从主活动开始,无论如何。我认为这是因为 category.LAUNCHER 属性。我试过 android:launchMode=singleTask,但没有任何区别;它总是从 Main 开始。
If someone could clarify intent filters, launch modes, and tasks, that would be great!
如果有人能澄清意图过滤器、启动模式和任务,那就太好了!
采纳答案by Bostone
I solved this by adding the screenless DispatcherActivityand making it the default one (by using the very same intent filter). In its onCreatemethod you create and call the Intent based on some reasonable default (your Main activity for example) OR based on some saved token that identifies which Activity should be started. That token is saved/refreshed in onStopmethod of any Activity you want to call on restart. You can save this token to Preferences.
我通过添加无屏幕DispatcherActivity并将其设为默认(通过使用完全相同的意图过滤器)解决了这个问题。在它的onCreate方法中,您根据一些合理的默认值(例如您的 Main 活动)或基于一些已保存的标记创建和调用 Intent,该标记标识应启动哪个活动。该令牌在onStop您要在重新启动时调用的任何活动的方法中保存/刷新。您可以将此令牌保存到首选项。
The rational here is that last activity that was visible will execute onStop method when interrupted.
这里的理由是,最后一个可见的活动将在中断时执行 onStop 方法。
Word of caution here: I did implement this pattern and it worked reasonably well. However it seems not play too well with history and finally I just gave up and yanked this code out. Nobody complained so far.
这里要注意的是:我确实实现了这种模式,而且效果还不错。然而,它似乎不太适合历史,最后我只是放弃并删除了这段代码。到目前为止没有人抱怨。
回答by James
FYI singleTaskis not what you want, since it starts a new task:
仅供参考singleTask不是你想要的,因为它开始了一项新任务:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
How are you launching Activity B? Any non-standard launch modes or Intent flags?
你如何启动 Activity B?任何非标准的启动模式或意图标志?
回答by Herr Grumps
For anyone coming here with similar problems, I found something strange that might be what you are seeing... maybe.
对于任何遇到类似问题的人,我发现了一些奇怪的东西,这可能是您所看到的......也许。
Say I have an app with activities A -> B -> C etc. I was having issues with my app always "resuming" to A if it was launched from the app list aka launcher. Resuming from the "resents" screen (long home press) would exhibit correct resume behaviour though (resume to B or C as expected). My manifest was nothing special, I have alwaysRetainTaskState="true" set in my root activity, and launch mode is default (standard).
假设我有一个带有活动 A -> B -> C 等的应用程序。如果我的应用程序是从应用程序列表(又名启动器)启动的,我的应用程序总是“恢复”到 A。虽然从“重新发送”屏幕(长按主页)恢复会表现出正确的恢复行为(按预期恢复到 B 或 C)。我的清单没什么特别的,我总是在我的根活动中设置 RetainTaskState="true",并且启动模式是默认的(标准)。
I was loading the apk onto my phone via a website. After downloading and installing, I would press "Open"to launch the app right away. For some reason (after uninstalling the app) I tired downloading again, installing, but then I pressed the "Done" button instead. Then Launching the app from the launcher/"all apps" list has the same resume behaviour as resuming from recents - in other words my problems were being caused somehow because of the installation process when clicking "Open" instead of "Done".
我正在通过网站将 apk 加载到我的手机上。下载并安装后,我会按“打开”立即启动应用程序。出于某种原因(卸载应用程序后)我厌倦了再次下载,安装,但后来我按下了“完成”按钮。然后从启动器/“所有应用程序”列表启动应用程序与从最近的应用程序恢复具有相同的恢复行为 - 换句话说,由于单击“打开”而不是“完成”时的安装过程,我的问题以某种方式引起。
I verified this "solution" on API10 (2.3.5) and API15 (4.0.4)
我在 API10 (2.3.5) 和 API15 (4.0.4) 上验证了这个“解决方案”

