简体   繁体   中英

Activity is getting destroyed while pressing the home button

In my application, when I press the home button the activity is going to onDestroy() . It suppose to be called onPause() method only right?

Why it is happening so?

also check that you don't use the android:noHistory flag in your manifest for the Activity

documentation: android:noHistory Whether or not the activity should be removed from the activity stack and finished (its finish() method called) when the user navigates away from it and it's no longer visible on screen

It depends on how much memory your phone has, if your phone does not have very much memory, then it will destroy the activity to free up resources immediately. On new phones, this will not happen because they have plenty of spare memory.

You activity could be destroyed upon pressing the home button if the system is constrained and has determined it needs to free some resources. The documentation states that onDestroy() can be called if:

This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Additionally, do note that the system can kill your program without calling onDestroy() after onStop() has been called. Therefore, any cleanup/data persistence code should be in either onPause() or onStop() .

Well, it depends on a lot of factors. If you are facing this issue on Android 3.2+ devices, you should add screenSize property to android:configChanges

    android:configChanges="keyboardHidden|orientation|screenSize"

Besides, also add android:launchMode="singleTop" to your launcher activity. Do note that you'd need to use Android SDK 15 or above as target, however, your app will work on older devices as well. Hope this helps.

要检查的另一件事是您的活动是否在onPause()时调用了finish() onPause()

当然可能是内存问题,但在此之前检查清单文件,在活动的声明中,如果您声明了“无历史记录”(您不希望活动保留在活动堆栈中。您也可以使用一些当您创建具有意图的活动时标记。那么,最有可能的答案是 Alex Contour 给出的答案。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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