Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

onDestroy() is not always called. If called, only part of the code is executed. And most of the time in LogCat I only see the message "gps state on destroy called first". Why is that?

protected void onDestroy() {     super.onDestroy();     Log.d("on destroy called", "gps state on destroy called first");      editor.putBoolean("gpsOn", false);     Log.d("on destroy called", "gps state on destroy called second");     editor.commit();      Log.d("on destroy called", "gps state on destroy called third");     stopRouteTracking();     Log.d("on destroy called", "gps state on destroy called  fourth"); } 
like image 866
ksu Avatar asked Aug 21 '13 15:08

ksu


People also ask

Is activity onDestroy always called?

onDestroy() is not always called. If called, only part of the code is executed.

What happens when onDestroy is called?

If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate() on that new instance in the new configuration. The onDestroy() callback should release all resources that have not yet been released by earlier callbacks such as onStop() .

What is onDestroy () activity?

onDestroy: The final call you receive before your activity is destroyed. 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. Here is an example...... public void onDestroy() { super.

When onDestroy () is called before onPause () and onStop () in an Android application?

onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.


2 Answers

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.

like image 125
Chris Avatar answered Oct 16 '22 11:10

Chris


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

like image 35
Karan Avatar answered Oct 16 '22 10:10

Karan