• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Will the onStop and onDestroy always get called?

onStop and onDestroy should ALWAYS get called.

That isn't correct. I don't think I can link to it because I haven't made enough posts yet but the relevant section of the docs is the API reference for android.app.Activity.

If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
And the table in the above mentioned page indicates that the process can be killed in those two states.

If the process is killed rather than being asked to finish those methods will not get called. That's why the docs tell you to save all the info you need to restore an activity during onPause.

How the applications the OP refers to behave is going to depend on how they terminate a running application. Do they ask nicely somehow or do they just kill the process? In any case your app shouldn't be reliant on onStop or onDestroy being called.
 
Upvote 0
Even if it is killed by the OS, onStop and onDestroy should still be called.

Note the "Killable" column in the above table -- for those methods that are marked as being killable, after that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed.

The quoted section is on the following page below the large table near the top of the page. Activity | Android Developers Short version: Pre-honeycomb onStop and onDestroy aren't guaranteed to run. Honeycomb onwards onDestroy isn't guaranteed to run.

narke: While you should be saving the state of your app in onPause the onStop and onDestroy methods are still useful.

Use onStop or onDestroy to make sure any threads you've started are stopped, message queues are cleared if your posting delayed messages between threads and any other cleanup that needs to be done so you don't leak views/threads/the whole activity.

If the whole process is killed none of these things matter as the whole process is wiped out.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones