Android:如何使启动器始终打开主活动而不是子活动?(或其他)启动器、或其他、而不是、Android

2023-09-07 10:06:43 作者:梦想的初衷!

我有活动 A 和 B.A 是带有 LAUNCHER 意图过滤器的活动(即当我们单击主屏幕上的应用程序图标时启动的活动).

I have activities A and B. The A is the one with LAUNCHER intent-filter (i.e. the activity that is started when we click the app icon on home screen).

A 使用 startActivity(new Intent(A.this, B.class)) 启动 B.

A launches B using startActivity(new Intent(A.this, B.class)).

当用户打开 B 活动,然后将我的应用程序置于后台,然后我的应用程序进程被杀死,当用户再次启动我的应用程序时,打开 B 而不是 A.

When the user has the B activity open, and then put my application into the background, and later my application's process is killed, when the user starts my application again, B is opened instead of A.

这导致我的应用强制关闭,因为 A 是初始化我的应用所需资源的 Activity,当 B 尝试访问未初始化的资源时,B 崩溃.

This caused a force close in my app, because A is the activity that initializes the resources my app needs, and when B tried to access the uninitialized resources, B crashes.

您有什么建议在这种情况下我应该怎么做?

Do you have any suggestions what should I do in this situation?

推荐答案

嗯,你真的应该让活动 B 也初始化你的资源.但是你可以把 android:clearTaskOnLaunch="true" 在您的清单中为活动 A 让启动器始终转到该活动.

Well you should really have activity B initialize your resources too. But you can put android:clearTaskOnLaunch="true" in your manifest for activity A to have the launcher always go to that activity.