Reputation: 459
When an Activity
goes in pause state, an instance of it remains in the Activity
stack that is managed by operating system. And I know after a while, it destroys the instance. Now my question is whether the onDestroy()
method is called when operating system destroys the Activity
instance after a long while?
Like if I put a Toast
inside onDestroy()
method, that will be shown when the Activity
instance gets destroyed by OS? (I know it will be shown by pressing back button).
The nature of this question makes it hard to test because sometimes it takes a day or more for OS in order to destroy an Activity
instance in stack.
Upvotes: 0
Views: 324
Reputation: 37604
There is no guarantee that it will be called. You can see Activity#onDestroy for that.
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. 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.
By simply killing it could be e.g. a System.exit
call or something similar where it skips the lifecycle hooks.
Upvotes: 1