Home > Articles > Mobile Application Development & Programming

  • Print
  • + Share This
This chapter is from the book

For the More Curious: Testing onSaveInstanceState(Bundle)

If you are overriding onSaveInstanceState(Bundle), you should test that your state is being saved and restored as expected. This is easy to do on the emulator.

Start up a virtual device. Within the list of applications on the device, find the Settings app (Figure 3.14). This app is included with most system images used on the emulator.

Figure 3.14

Figure 3.14 Finding the Settings app

Launch Settings and select Developer options. Here you will see many possible settings. Turn on the setting labeled Don’t keep activities, as shown in Figure 3.15.

Figure 3.15

Figure 3.15 Don’t keep activities selected

Now run your app and press the Home button. Pressing Home causes the activity to be paused and stopped. Then the stopped activity will be destroyed just as if the Android OS had reclaimed it for its memory. Then you can restore the app to see if your state was saved as you expected. Be sure to turn this setting off when you are done testing, as it will cause a performance decrease and some apps will perform poorly.

Pressing the Back button instead of the Home button will always destroy the activity, regardless of whether you have this development setting on. Pressing the Back button tells the OS that the user is done with the activity.

To run the same test on a hardware device, you must install Dev Tools on the device. For more information, visit http://developer.android.com/tools/debugging/debugging-devtools.html.

  • + Share This
  • 🔖 Save To Your Account