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

3

42

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");
}
Kush answered 21/8, 2013 at 15:34 Comment(8)
Have you tried putting this code on onStop instead of onDestroy? onStop is called right before onDestroy. And, also, onDestroy shouldn't run codes that might take a lot of time.License
developer.android.com/training/basics/activity-lifecycle/… Check the Activity Lifecycle from the Official DocsBalmuth
@HugoHidekiYamashita, I haven't try to put it on onStop. I will try it and remove the stopRouteTracking() to see how it work.Kush
@Pavlos, from the official doc, there's :Note: do not count on this method being called as a place for saving data! so I think onDestroy() is not called every time.Kush
That's what everyone said here!Balmuth
Can any one tell me what to do if ondestroy and onstop r not reliable?Merciless
move super.onDestroy() to the last line of the method.Calcareous
the invocation of the superclass onDestroy() should always be the very last statement in your onDestroy() handler otherwise you may experience bugs and unwanted side effects.Baun
M
29

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.

Malvina answered 21/8, 2013 at 15:40 Comment(6)
However, why when onDestroy is called, only the first couple of lines are executed.Kush
I can only speculate that the lines beyond the first LogCat might be throwing an exception, but I don't know what the rest of the log looks like.Malvina
onStop() is not guaranteed to be called, either: developer.android.com/reference/android/app/…Kailey
@Kailey For practical purposes (the overwhelming majority of the time), however, it's safe to assume onStop() will be called as expected, correct?Eggshaped
@Kartik Chugh Can you post more info about that as this activity lifecycle diagram -developer.android.com/images/activity_lifecycle.png - shows an app process can be killed straight after onPause() - ie, without onStop() (or, therefore, onDestroy()) being called.Kailey
@Ksu editor.commit() is a blocking call that accesses the storage. Therefore, it might be long enough for the system to stop waiting and kill your app halfway through onDestroy. I suppose that stopRouteTracking() was the missing call?Hartung
S
17

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

Salvatore answered 24/9, 2016 at 7:51 Comment(0)
S
3

@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.

Sachsse answered 11/7, 2016 at 0:18 Comment(3)
I was wondering if this could be the issue, too. But does it adequately explain why only one line of code after super.onDestroy() is executed?Kailey
Yes, of course. It might be worth updating your answer with your revision of the OP's code snippet in order to help illustrate.Kailey
also won't get called most of the time, tried calling it at the end of the my code but nothing new.Landloper

© 2022 - 2024 — McMap. All rights reserved.