When the user leaves an activity, Android moves it to the recent apps. When Android needs more memory, it "kills" the app - unloads all Activity objects and will no longer exist. The user can still click on the app in the recent apps which starts the activity where the user left it. This often causes nullPointerException. We can prevent it by saving data with SharedPreferences, database or savedInstanceState.
How can I test whether I implemented data saving correctly without waiting on Android to unload objects of activity? i.e. how can I force Android to "kill" my activity?
There's a section called Apps
in the Developer options
. Make sure Do not keep activities
is on, it'll allow you to test this behavior. Just don't forget to turn it off when you're done or you will be debugging weird lifecycle behavior in your app later.