Would the onDestroy method be called when the phone runs out of battery? I would imagine that it doesn't. Unfortunately, I don't have a phone to test this on right now, otherwise I would just check for myself.
In reality there is no telling what will happen. There's even doubt about if onDestroy()
will be called under normal circumstances.
One of the few situations where onDestroy()
in an Activity
SHOULD be called is when using the BACK
button or if an event in the Activity
explicitly results in calling finish()
. In result, and in theory, the Activity
will be stopped (onStop()
is called) then destroyed (onDestroy()
is called).
The ambiguity WRT the clean shutdown of app components comes when processes are actually 'killed' - this is why Task Killers are so evil as they basically forcibly rip the process out of memory regardless of running state and usually prevent any cleanup operations to occur.
In an absolute emergency (such as when the battery is at absolute minimum), the system will do its level best to shut down any running processes as cleanly as possible but there's no guarantee it will do this successfully.
In particular, if you have any 'mission critical' data or state which needs to be saved then do it when an Activity
is paused (i.e., in onPause()
or at the very least when it is stopped (in onStop()
).
I personally rarely make use of onDestroy()
for the reasons I've outlined above - in general I work on the creation -> start -> resume -> pause -> stop - restart life-cycle methods.
Finally, you have to remember your app may have a number of different components (Activities, Services and either an explicit or implicit Application) - in the case of a low battery shutdown, each of these components may be handled differently. All in all, however, if the process is 'ripped' ot of memory and forcibly stopped, there's no guarantee of what you'll find once you've re-charged the battery and restarted your app unless you plan for saving data and / or state.