3

When I want to save some state behavior of the activity, The docs says that we should implement OnSaveInstanceState and OnReceiveInstanceState.

They say that this will save the activity state even after destroy or restarts. I care more about destroy (the activity is completely gone) , does that mean the bundles are considered persistent ?

when I open a pdf reader, clost it and open it again i see that it opens in the same page I was in. is this implemented using Bundles or oth

M Hassen
  • 363
  • 1
  • 7
  • 17

6 Answers6

2

To store persistent application data use Shared Preferences. Shared Preferences are simply sets of data values that are stored persistently. By persistence, we are talking about data that persists across application lifecycle events. In other words, the application (or device, for that matter) can be started and stopped without losing the data. The next time the user launches the application, that data will still be available. Some Games use Shared preference for exemple to store the level of the game reached, the player's name ... see this link to learn how to use Android Preference API

Preference are simular to bundles However they are persistant and bundle are not!! Keep in mind that if you need to store persistent data you have 4 options to do this:

  1. Using Shared Preferences
  2. Using SQLite Databases
  3. Using Internal Storage
  4. Using External Storage

Bundles are not persistent, the documentation says not to count on it, onSaveInstanceState() is called when the activity is about to be killed by the system, but for a known restart (for instance on a screen rotation.) If your activity is killed because the system needs more resources (while the activity is in the background), onSaveInstanceState() will not be called, but onPause() will. onSaveInstanceState() really is not meant to save persistent data, as the doc states.

K_Anas
  • 31,226
  • 9
  • 68
  • 81
1

Bundles are not persistent, and the documentation for them specifies that using them for persistence is not a good idea as their internal format may change between devices or even OS versions.

SharedPreferences, on the other hand, can be persisted and are the recommended way to store information like current app state.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
1

Some relevant parts from SavingActivityState :

Note: There's no guarantee that onSaveInstanceState() will be called before your activity is destroyed, because there are cases in which it won't be necessary to save the state (such as when the user leaves your activity using the Back button, because the user is explicitly closing the activity).

There is no guarantee data will be stored, especially in the case where your user exits the app.

Note: Because onSaveInstanceState() is not guaranteed to be called, you should use it only to record the transient state of the activity (the state of the UI)—you should never use it to store persistent data. Instead, you should use onPause() to store persistent data (such as data that should be saved to a database) when the user leaves the activity.

So like K-ballo said, used SharedPreferences if you have persistent data to store. onSavedInstanceState() is mostly useful for storing UI related data.

telkins
  • 10,440
  • 8
  • 52
  • 79
1

You can sorta consider SavedInstanceState() permanent but it's not recommended to use it for saving application related data in a persistent manner as It's not guaranteed to be called and it's not recommended by the authors themselves.

so, Only use them for saving user interface state changes (background color, currently selected items ,..) and use other method for persistence like : SharedPreferences, Files and SQLite.

MSaudi
  • 4,442
  • 2
  • 40
  • 65
0

As every one else has recommended use shared preference and you should do this saving in onDestroy and onSavedInstance both.

When android is going to run low on memory, its just going to kill your application and call your onSavedInstance without calling onDestroy etc. Save your context in bundle it passes in onSavedInstance. When your app comes in foreground again, android will take care of restoring your back stack of activities. But this time it will pass you the bundle in your onCreate for each activity which will have all the values you saved in your onSavedInstance while your app was getting killed.

Hope this helps.

anargund
  • 3,249
  • 2
  • 21
  • 25
0

Short answer: It's not permanent.

Long answer: From "Android Programming - The Big Nerd Ranch Guide":

When onSaveInstanceState(...) is called, the data is saved to the Bundle object. That Bundle object is then stuffed into your activity’s activity record by the OS

...

So when does the activity record get snuffed? When the user presses the Back button, your activity really gets destroyed, once and for all. At that point, your activity record is discarded. Activity records are also typically discarded on reboot and may also be discarded if they are not used for a long time.

Community
  • 1
  • 1
Hải Phong
  • 5,094
  • 6
  • 31
  • 49