How Can I Know An Activity Is Going To Be Killed By Os?
Solution 1:
Per the docs, onDestroy
should be called right before the Activity is destroyed, regardless of the reason. If the finish was requested, isFinishing
will return true. So if it is false, you can assume that the system needed to finish.
However, as the docs also say
Note: do not count on this method being called as a place for saving data!
In general, you cannot guarantee that your Activity will be killed nicely. Things like task killers mess with the lifecycle.
Use onPause
or onSaveInstanceState
to save things properly.
Solution 2:
You cannot.
It's possible your activity could go away without the rest of your app going way, in this case onDestroy would be called. However it's also possible that your whole app is going to get killed at once, this like a kill -9 in unix. Your app cannot run any code at this time, it's killed instantly and without warning.
To handle this properly, you want to design your app to save all vital information to disk in onPause and be ready to retrieve it later in onCreate if needed.
Post a Comment for "How Can I Know An Activity Is Going To Be Killed By Os?"