Android Activity onDestroy() is not always called and if called only part of the code is executed Android Activity onDestroy() is not always called and if called only part of the code is executed android android

Android Activity onDestroy() is not always called and if called only part of the code is executed


Take a look at this:

Activity OnDestroy never called?

And this:

http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

Basically, there's never a guarantee that onDestroy() will be called, and in some cases processes such as your app will be killed directly, bypassing the method call anyway.


In the android developer documentation here, you can see that -

for those methods that are marked as being killable, after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.

and onStop() and onDestroy() both are marked as killable.

This may be the reason that only part of the code written in onDestroy() is being called since process can be destroyed at any time after it executes onStop().


@Chris's answer is correct, however your issue where only part of your code is called can arise from the call to super.onDestroy() before calling your code. super.onDestroy() should be called at the end because then your code will be called before it is destroyed.