No, there is no guarantee that the system will always call onDestroy, when it needs to free resources or when someone gives a -9 kill on you, for example.
void onDestroy ()
"It performs any final cleanup before an activity is destroyed. This may be because the activity is terminating, or because the system is temporarily destroying this activity instance to save space.
There are situations where the system simply kills the process that hosts the activity without calling this method (or any other) , so it should not be used to do things that should remain after the process disappears . "
"If an error occurs, you want to save the user's actions before closing completely"
If this is your goal then set a UncaughtExceptionHandler
, put it in the first instance of your application's activity:
Thread.UncaughtExceptionHandler oldHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread thread, Throwable exception) {
// Lide com o erro aqui
}
if (oldHandler != null)
oldHandler.uncaughtException(thread, exception);
else
System.exit(2);
}
});
You may want to put this code in more than one activity to try to handle errors differently, I do not recommend, if you do, that same code will run more than once and at the same time in the event of a crash.
Reference