user3398676
user3398676

Reputation: 11

Why should I override onSavedInstanceState for retrieve instance state in android?

In onCreate(Bundle savedInstanceState), there`s already super.onCreate(savedInstanceState).

API says it restores states when create activity after destroyed.

But I have to override onSavedInstanceState(Bundle outState) for restore specific states.

Why?

What kind of informations are saved in savedInstanceState with method onCreate() and onSavedInstanceState()?

I'm so confused!

Upvotes: 0

Views: 372

Answers (2)

MatteoM
MatteoM

Reputation: 158

onSaveInstanceState() is called before your activity is paused. So any info that it needs after it is potentially destroyed can be retrieved from the saved Bundle. The Bundle is a container for all the information you want to save. You use the put* functions to insert data into it. To get the data back out, use the get* functions just like the put* functions. The data is stored as a name-value pair. There isn't a specific use of this element, you can use it in any case (save a name, a number or whatever you need to have again when the use open again the app)

Upvotes: 0

agamov
agamov

Reputation: 4427

By default, when your device changes configuration (for example, devices rotates, you changed language settings, etc.), your foreground Activity is recreated, and all you Activity data is lost. For example, if you have a member variable mVariable that was assigned some value, after configuration change you will lose its value. That's why you need to save important data to savedInstanceState and re-init it from onCreate() method. You simply check whether savedInstanceState is not null, and if so, you init your values from savedInstance, else - init with default values.

Further reading: http://developer.android.com/training/basics/activity-lifecycle/recreating.html

Upvotes: 0

Related Questions