Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is onPause() or onStop() called even if phone dies due to lack of power?

If I, for example, need to keep some very important data which the user can edit within my app, should I need to save them every time user changes such data or is it ok if I would save it within onPause(), onStop() or onDestroy() methods?
Can somehow application end without any of those methods calling? (For instance when battery runs out)

like image 911
Austin Johnson Avatar asked Oct 28 '16 11:10

Austin Johnson


2 Answers

In that scenario when the phone is shutting down, you can use the ACTION_SHUTDOWN Intent. More info here. For all other cases onPause should do the work. Even in the link provided, there is a statement that onPause will be called if the app is in FG.

Apps will not normally need to handle this, since the foreground activity will be paused as well.

However, if it is not so expensive operation, I would go with saving data after edit.

like image 38
Drez Avatar answered Nov 15 '22 19:11

Drez


This certainly can't be done in onDestroy(). According to the 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.

So yes, the application can end without calling any of the lifecycle methods.

like image 167
Wilder Pereira Avatar answered Nov 15 '22 19:11

Wilder Pereira