I'm confused about Activity.onDestroy()
. I need to free some resources when my Activity is destroyed, but it seems like onDestroy() is called just when i press "Back" key, but not when my Activity is killed by Dalvik. I tested it just by adding log:
Log.v("my_tag", "onDestroy() called");
and the same in onCreate() method too:
Log.v("my_tag", "onCreate() called");
Then i start my Activity, and i see in logs: onCreate() called
. I press "Back" key and then start Activity again, then i see:
onDestroy() called
onCreate() called
Then i press "Home" key and go to my Activity again, logs does not change. All right here.
Then i press "Home" key again and start some really "heavy" applications. In logs is nothing about onDestroy(), but when i start my Activity again, i see in logs: onCreate() called
! So, onDestroy() was not called, but my Activity was killed. What's wrong?
onDestroy( ) is called before the activity is destroyed. The system invokes this callback either because: the activity is finishing (due to the user completely dismissing the activity or due to finish( ) being called on the activity), or.
onDestroy() is not always called. If called, only part of the code is executed.
onDestroy() is a method called by the framework when your activity is closing down. It is called to allow your activity to do any shut-down operations it may wish to do.
After `onDestroy()` is called, the OS knows that those resources are discardable, and it starts cleaning up that memory. Your activity may also be completely shut down if your code manually calls the activity's finish() method, or if the user force-quits the app.
From the onDestroy() documentation:
[..] There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
In Android, you won't get a guaranteed onDestroy()
call.
If you want to release some resources, you should do that in onPause()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With