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.This app is included with most system images used on the emulator.

Figure 3.15

Figure 3.15 Finding the Settings app

Launch Settings and select Development options.Here you will see many possible settings.Turn on the setting labeled Don't keep activities.

Figure 3.16

Figure 3.16 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.

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, visithttp://developer.android.com/tools/debugging/debugging-devtools.html.

  • + Share This
  • 🔖 Save To Your Account