One of the scenarios I admittedly ~almost~ always forget to test is “What happens when my app goes into the background, then the OS kills is to claim memory, then I try to resume?” Usually it’s “Well, I handle onSavedInstanceState
not being null
, so I am great!” It is fine and dandy for simple apps; but once your Activity
or Fragment
gets beefier and you start relying on state for more and more things, it can get complicated pretty quickly (In my case, the Fragment
has setRetainInstance(true)
).
This scenario in particular is kind of hard to reproduce willingly. I usually see this when I leave an app running, make my phone do some heavy work overnight, then resume the app the next day.
It turns out that Android Studio has the answer! There is this magical tiny red button that allows you to simulate this exact scenario.
- Open your app to the
Activity
you want to test (I use a very simple app here just for demo).
5. Resume your app. I usually just do this via recent apps.
6. If you look at Studio, you'll see that your app is now no longer dead, but has a new process ID, in this case `26742`.
If at this point you step through your code, you will notice that your `Activity` will go through the whole (re-)creation process with the `Bundle` given the values you have saved in `onSaveInstanceState`. No more waiting overnight, yay!