Activity is getting destroyed while pressing the home button
Asked Answered
E

6

13

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?

Element answered 14/1, 2013 at 6:39 Comment(6)
Can you add some code? If you explicitly command onPause (or even onDestroy), then your problem will mostly likely come from that area. Use the search function.Intercession
Look at these answers: https://mcmap.net/q/903492/-when-i-return-back-to-my-activity-oncreate-is-raised-instead-of-onactivityresult https://mcmap.net/q/903493/-onactivityresult-sometimes-not-called-when-sub-activity-finishes Maybe you have same problem. Go to setting-> developer options unchecked Don't keep activities and Background process limit set to standard limit. If Don't keep activities is checked, the state of Activities are not kept so when you leave an Activity it is destroyed. Enjoy!Obara
is the nohistory attribute true for your activity? if so then activity finish will be calledCigar
android:noHistory defaults to falseLonghand
@breceivemail you saved my life !Parma
@Cigar Thank you so much for saving my life. But I think noHistory="true" doesn't call finish(). I checked with printing isFinishing in onPause and onStop, but both of them showed false. Did I check incorrectly?Brecciate
J
12

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.

Jussive answered 14/1, 2013 at 6:42 Comment(0)
P
15

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

Pence answered 20/10, 2015 at 13:56 Comment(1)
You are absolutely right, but just a quick question: I think noHistory="true" doesn't call finish(). I checked with printing isFinishing in onPause and onStop, but both of them showed false. Is finish() not called?Brecciate
J
12

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.

Jussive answered 14/1, 2013 at 6:42 Comment(0)
F
6

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().

Frizzly answered 14/1, 2013 at 6:45 Comment(0)
T
1

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.

Trepang answered 26/1, 2013 at 4:3 Comment(0)
P
1

another thing to check is whether your activity call finish() when onPause()

Parthen answered 28/4, 2018 at 7:31 Comment(0)
C
1

Of course may be memory problems, but before that check the manifest file, in the declaration of the activity, if you have "no history" declared (you don't want the activity to remain in the activity stack. Also you may using some flag when you create the activity with an intent. Then, most probable answer is the one given by Alex Contour.

Centare answered 24/5, 2018 at 18:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.