tags:

views:

75

answers:

1

I'm having a hard time figuring out the best way to pass simple values from onPause and onResume in the Android activity lifecycle. I understand how to use get and put extra bundles for activity to activity data, but does that work for passing data between the same activity? Should i used SharedPreferences?

+2  A: 

If you just want to store the transient state of your application override onSaveInstanceState() and onRestoreInstanceState().

These aren't lifecycle methods so are not always called. They are used by the system so an Activity looks like how the user left it if they go away and come back to it. If the User explicitly terminates the application - for example, by pressing the Back button - they are not called, so you should not use them for writing persistent user data.

If you want to record persistent data you have several options:

  • Shared Preferences
  • Internal Storage
  • External Storage
  • SQLite Databases

The one to use will depend on the data you want to store.

Dave Webb
Also, for the specific case of screen rotations, there is `onRetainNonConfigurationInstance()` and `getLastNonConfigurationInstance()`.
CommonsWare