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

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

问题描述

熟悉的场景:我有一个 Main Activity,它在按下按钮时启动 Game Activity.如果用户按下 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 的另一个实例并将其添加到该应用程序的堆栈中,而不是仅仅选择顶部的任何内容,因为如果我在重新启动应用程序后按 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.

我一直在阅读并重新阅读有关活动、任务和堆栈的文档并浏览 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 类别的 Intent 启动应用程序.安装程序会启动一个带有 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.

Ref: 应用总是启动从根活动新鲜而不是恢复后台状态(已知错误)

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

而且我猜想之前按下快捷方式时会创建一个新的活动实例的原因是由于使用了不同的 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天全站免登陆