How to test code built to save/restore Lifecycle of an Activity?

To test onSaveInstanceState and onRestoreInstanceState you can use either the SetAlwaysFinish tool (see link below) or the DevTools app included with the emulator.

http://bricolsoftconsulting.com/how-to-test-onsaveinstancestate-and-onrestoreinstancestate-on-a-real-device/

Both of these apps use a hidden setting called AlwaysFinish which is part of the ActivityManagerNative class to change the behavior of the Android OS. Under the new behavior, the OS will unload any activity as soon as it leaves the screen, triggering the onSaveInstanceState event. When the OS wants to bring the activity back, it will call the onRestoreInstanceState event.

The link above explains how to use the SetAlwaysFinish app to test your app's onSaveInstanceState and onRestoreInstanceState events. If you want to use the DevTools, then enable Development Settings > Immediately destroy activities.


If you have enabled Developer Options on your device, there is an option Do not keep activities that will help test onRestoreInstanceState().

Android Settings panel


We had an issue whereby re-launching an app after long periods of inactivity crashed. We found that "Don't keep activities" wasn't reproducing the issue, but Background process settings :: No background processes in Dev Settings did (even while debugging).