按HOME后重新启动应用程序时如何返回最新启动的活动? [英] How to return to the latest launched activity when re-launching application after pressing HOME?

查看:32
本文介绍了按HOME后重新启动应用程序时如何返回最新启动的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

熟悉的场景:我有一个 Main 活动,当按下按钮时它会启动一个 Game 活动.如果用户按下 HOME,然后再次启动我的应用程序,它应该会显示 Game 活动,这是他在使用应用程序时最后做的事情.

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.

然而,发生的事情是他再次获得了 Main 活动.我感觉 Android 正在创建 MainActivity 的 another 实例并将其添加到该应用程序的堆栈中,而不是仅仅选择顶部的任何内容,因为如果我在重新启动应用程序后按 BACK,我进入游戏活动!并且每次调用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 几乎是骨干":

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 中开箱即用",正如对一个非常相似的问题的回答所说:在Android中维护标准应用Activity回栈状态(使用singleTask启动模式),但不是.

In theory this should work in Android just "out of the box", as the answer to a very similar question says: Maintaining standard application Activity back stack state in Android (using singleTask launch mode), but it isn't.

我一直在阅读和重新阅读有关 Activity 和 Task 和 Stacks 的文档,并浏览了 SO 中的所有相关答案,但我不明白为什么这么简单的设置不能按预期工作.

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 使用没有操作和类别的意图启动应用程序.Launcher 使用具有 android.intent.action.MAIN 动作和 android.intent.category.LAUNCHER 类别的意图启动应用程序.安装程序使用 android.intent.action.MAIN 操作启动一个应用程序,并且没有类别.

This is due to the intents being used to start the app being different. Eclipse starts an app using an intent with no action and no category. The Launcher starts an app using an intent with android.intent.action.MAIN action and android.intent.category.LAUNCHER category. The installer starts an app with the android.intent.action.MAIN action and no category.

参考:应用程序总是启动从根活动中恢复而不是恢复后台状态(已知错误)

所以我手动关闭了手机中的应用程序,然后从主屏幕小部件重新启动它.然后打开GameActivity,然后按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.

而且我猜想之前按下快捷键时会创建一个新的 Activity 实例的原因是因为启动 Activity 时使用了不同的 Intent.

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.

这篇关于按HOME后重新启动应用程序时如何返回最新启动的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆