问题描述
熟悉的场景:我有一个的主的活动,会启动的游戏的活动时,一个按钮是pressed。如果用户presses HOME,然后重新启动我的应用程序,它应该是psented用的游戏的活动,这是使用应用程序时他在做什么最后$ P $
Familiar scenario: I have a Main activity that launches a Game activity when a button is pressed. If the user presses HOME, and then launches my application again, it should be presented with the Game activity, which is what he was doing last when using the application.
然而,发生了什么的却是他再次得到的主的活动。我有机器人正在创建的感觉的 MainActivity的另一个的实例,并在添加到堆栈该应用程序,而不是只挑选任何是在上面,因为如果我preSS的返回重新发起后应用程序,我得到的游戏活动!而Main.onCreate方法被调用,而不是调用GameActivity.onResume每次。
However, what happens instead is he gets the Main activity again. I have the feeling that Android is creating another instance of MainActivity and adding it to the stack for that application, instead of just picking whatever was on the top, because if I press BACK after relaunching the app, I get to the Game activity! And the Main.onCreate method is called every time, instead of calling GameActivity.onResume.
我的的AndroidManifest.xml
是pretty的很多'裸露的骨头:
My AndroidManifest.xml
is pretty much 'bare bones':
<activity android:name="MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="GameActivity" android:label="@string/app_name">
</activity>
正如你所看到的,没有什么太花哨。
As you can see, nothing too fancy.
这是如何在新的活动推出,很简单也:
And this is how the new activity is launched, very simple too:
Intent intent = new Intent(this, GameActivity.class);
startActivity(intent);
在理论上这应该工作在Android中只是开箱即用,因为回答一个非常类似的问题说:Maintaining在Android的应用程序的状态,但事实并非如此。
In theory this should work in Android just "out of the box", as the answer to a very similar question says: Maintaining application state in Android, but it isn't.
我一直在阅读和重新阅读活动和任务以及堆栈的文档和浏览的,因此所有相关的答案,但我不明白为什么这样一个简单的设置并不完全按预期工作。
I've been reading and re-reading the documentation on Activity and Task and Stacks and browsing all related answers in SO but I can't understand why such a simple setup is not quite working as expected.
推荐答案
呵呵,我想我已经找到了答案。
Oh, I think I've found the answer.
由于我用的IntelliJ启动应用程序,现在看来,这将启动以不同的方式不是用户的应用程序,点击主屏幕小工具,将推出。它的答案解释到另一个SO问题:
Because I was launching the app by using IntelliJ, it seems it launches the application in a different way than a user, clicking a home screen widget, would launch. It's explained in the answer to another SO question:
这是由于所使用的意图来启动应用程序是不同的。 Eclipse启动使用,没有行动,没有类别的意图的应用程序。该发射器开始使用与android.intent.action.MAIN行动,android.intent.category.LAUNCHER类别意图的应用程序。安装程序启动与android.intent.action.MAIN行动,并没有任何类别的应用程序。
编号:App总是开始从新鲜根系活力,而不是恢复的背景状态(已知错误)
所以我手动杀在手机中的应用,并从主屏幕小部件再重新启动它。然后打开GameActivity,和pressed HOME。现在,重新发动时,它的GameActivity仍然是可见的,并保持它的UI状态,因为它是我离开的时候了。
So I've manually killed the application in the phone, and relaunched it again from the home screen widget. Then opened the GameActivity, and pressed HOME. Now, when relaunching it the GameActivity is still visible and keeping its UI state as it was when I left it.
和我想,为什么活动的新实例被创建时,之前曾是pressing快捷由于不同的意图的原因被用于启动该活动。
And I guess that the reason why a new instance of the activity was created when pressing the shortcut before was due to a different Intent being used for starting the activity.
这篇关于如何重新启动pressing回家后的应用程序时,返回到最新推出的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!