Reputation: 369
In my application, I have a feature that allows a user to update a group of personalized app settings, triggered by a boolean flag called useNew. Keeping the possibility of an interruption in mind, I reset useNew to false if a) the user cancels the operation or b) the activity processing this logic is killed unexpectedly. I put the reset logic in onStop since Android specifies that, post-Honeycomb, it's the last lifecycle event guaranteed to be called: https://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
This has been working fine in all circumstances except for one. If I run the app in Studio and then, with the app still open, try to run it from Studio again, I get this warning message that this will kill my session.
Choosing "Restart app" closes out of my activity without calling onStop. My flag is never reset and on the next app run, the logic fails.
My question: Is this behavior a singular occurrence in Studio, or is it possible for onStop to be skipped under rare circumstances?
Upvotes: 0
Views: 377
Reputation: 7919
Are you sure that onStop is not being called? What could be happening is that onStop is being called but the corresponding code is not being run. In Android Studio when you force restart the app, it instantly terminates your application.
From the Google documentation, onStop is a killable method.
for [killable methods], after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed.
Instead, any code that persists data (i.e. autosaving user data), should be placed in onPause().
You should use the onPause() method to write any persistent data (such as user edits) to storage.
Upvotes: 1
Reputation: 15775
When Android Studio kills the app to restart it, the action is forceful, so it does not adhere to any of the lifecycle semantics. It does not go through the "normal" mechanisms which drive Activity
lifecycle state. Think of it like running kill -9
for the process hosting your app.
Upvotes: 1