0

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!

  • See http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please for a good explanation of what you do in the functions when you override them – Christian Garbin Mar 30 '14 at 11:01

2 Answers2

0

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

agamov
  • 4,407
  • 1
  • 27
  • 31
0

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)

MatteoM
  • 158
  • 1
  • 10