Sunday, April 28, 2013

Simulate kill activity to test onSaveInstanceState() / onCreate()

In order to test the lifecycle behaviour of our apps, we always have to leave our apps to move it to background, wait sometime for the system to kill it...and restart it.

It's a alternative to simulate "kill activity" by the Immediately destroy activities (or called Don't keep activities/Do not keep activities) selection Development Settings:

Select Developer options in Setting
Select Developer options in Setting

Select Advanced

Check Don't keep activities


According to the Android Developer document of Using the Dev Tools App:

Immediately destroy activities
Tells the system to destroy an activity as soon as it is stopped (as if Android had to reclaim memory). This is very useful for testing the onSaveInstanceState(Bundle) / onCreate(android.os.Bundle) code path, which would otherwise be difficult to force. Choosing this option will probably reveal a number of problems in your application due to not saving state. For more information about saving an activity's state, see the Activities document.


Notice: This setting for developers only, NOT for general user.



Related: The lifecycle in Activity Killed

No comments: