简体   繁体   English

当按下主页按钮时,后台活动被杀死

[英]Activity in background gets killed when Home button is pressed

I encountered strange problem, lets say I have two activities A and B, app starts with Activity A, I proceed to activity B press Android Home Button, return to app which brings me back to Activity B. Then I press Back button (either hardware on in toolbar) and this closes app, but it should return me to Activity A. Activity B has no override of onBackPressed and has Activity A stated as PARENT_ACTIVITY in manifest. 我遇到一个奇怪的问题,可以说我有两个活动A和B,应用程序以活动A开始,我继续进行活动B,请按Android主页按钮,返回到使我回到活动B的应用程序。然后按“返回”按钮(两个硬件工具栏上的),这将关闭应用程序,但应返回到活动A。活动B没有覆盖onBackPressed,并且活动A在清单中声明为PARENT_ACTIVITY。 I'm starting it with Intent with no flags. 我从没有标志的Intent开始。 Any idea why this happens ? 知道为什么会这样吗? Thanks 谢谢

In your activity A when you call your activity B, maybe you have the following command : 在活动A中,当您调用活动B时,也许您具有以下命令:

finish();

If yes, you should remove this line. 如果是,则应删除此行。 Then when you press back key from your activity B you should return A. If not, maybe try to share your code. 然后,当您在活动B中按返回键时,您应该返回A。否则,请尝试共享您的代码。

The behaviour of back buttons depends on system version. 后退按钮的行为取决于系统版本。 There is support for providing back navigation in older Android versions, described here: 支持在较旧的Android版本中提供向后导航,如下所述:

https://developer.android.com/training/implementing-navigation/ancestral.html https://developer.android.com/training/implementing-navigation/ancestral.html

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"
        android:parentActivityName="com.example.myfirstapp.MainActivity" >
        <!-- Parent activity meta-data to support 4.0 and lower -->
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myfirstapp.MainActivity" />
    </activity>
</application>

The best and most convenient way to debug back stack issues is to enable 'Don't keep activities' option in developer options. 调试后堆栈问题的最佳,最方便的方法是在开发人员选项中启用“不要保留活动”选项。

That's my best guess. 那是我最好的猜测。 Good luck! 祝好运!

In order to run a new activity without destroying the old one, you have to add the flag FLAG_ACTIVITY_NEW_TASK to the intent that will run the activity: 为了在不破坏旧活动的情况下运行新活动,您必须将标志FLAG_ACTIVITY_NEW_TASK添加到将要运行该活动的意图中:

 Intent intent = new Intent(MainActivity.this, MainActivity2.class);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 startActivity(intent);

as when setting this flag: 设置此标志时:

this activity will become the start of a new task on this history stack. 此活动将成为此历史记录堆栈上新任务的开始。 A task (from the activity that started it to the next task activity) defines an atomic group of activities that the user can move to. 任务(从启动它的活动到下一个任务活动)定义了用户可以移动到的原子活动组。 Tasks can be moved to the foreground and background; 任务可以移到前台和后台; all of the activities inside of a particular task always remain in the same order. 特定任务中的所有活动始终保持相同的顺序。

so the activity which started it will remain in the stack, and hence you can call it again, and hence it also can be called automatically again when pressing BACK_BUTTON even if you pressed the HOME_BUTTON earlier. 因此启动该活动的活动将保留在堆栈中,因此您可以再次调用它,因此即使您之前按了HOME_BUTTON ,也可以在按BACK_BUTTON时再次自动调用它。


and you have to combine @gduh answer with mine, as for sure you must make sure that you are not calling finish(); 并且您必须将@gduh答案与我的结合起来,以确保您必须确保未调用finish(); ;。 in ActivityA while calling the ActivityB . ActivityA同时调用ActivityB

感谢您的帮助,问题是由清单android:launchMode = singleinstance中的活动标志引起的(它最初不是我的项目,所以我错过了,我只是希望我不要通过删除它来搞砸其他事情)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM