2

I'm trying to understand life cycle methods and onSaveInstanceState().

Is it correct that I actually only need to worry about onCreate() where I set up whatever my activity requires, and onPause() where I should save a copy of any of my own data into persistent storage to be restored later in onCreate()?

Can/Should I just ignore onSaveInstanceState()? I notice that some samples add data into the bundle, but if one is hardening data to persistante storage elsewhere why would I want to do this too?

Ian

Ian
  • 1,507
  • 3
  • 21
  • 36
  • Been struggling with similar issue and found http://stackoverflow.com/questions/8784610/onsaveinstancestate-and-onpause-call-sequence. The takeway is that onSaveInstanceState is not guaranted (which is what the android doc says) but the onPause() callback is, and hence save state with onSaveInstanceState().put() there. When app has to recreate activity, onCreate() can do so from onSaveInstanceState().get() which we know was initiated in onPause(). – Henry Thornton Apr 02 '12 at 16:13

1 Answers1

1

onSaveInstanceState() is used to store state when the activity goes to onStop() and use onRetrieveInstanceState() or onCreate() to get previous state.

But if the app is killed in background then you can't maintain state. At those scenarios Shared Preferences will be better than this.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
  • You could also persist data using a database or ContentProvider. – louielouie Mar 14 '12 at 07:00
  • Thanks guys, but that's not really answering the questions, viz: 1. Do I need to have code to save my own data in any other methods except onCreate and OnPause? 2. Under what circumstances do I need to add data into the SaveInstanceState bundle? But thanks for your replies anyway. – Ian Mar 16 '12 at 19:56