0

so i get the main idea of how to use

  protected void onSaveInstanceState (Bundle outState)

http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

also from Saving Android Activity state using Save Instance State

but my problem is what if this was the first time the application is being created? then nothing would have been stored in the bundle before....and if so then when i try to call something out from the bundle that hasn't been saved before what do i get?null? for example i have this in my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String [] b=savedInstanceState.getStringArray("MyArray");
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    String [] a={"haha"};
    savedInstanceState.putStringArray("MyArray", a);
}

in the FIRST time the application is ever opened what would the value of b be? and after the application has been used once what would the value of b be?

Many thanks!

Community
  • 1
  • 1
Ali Elgazar
  • 777
  • 2
  • 12
  • 26

2 Answers2

3

in your onCreate() add a condition

 if(savedInstanceState==null){
  //meaning no data has been saved yet or this is your first time to run the activity.  Most likely you initialize data here.
 }else{
   String [] b=savedInstanceState.getStringArray("MyArray");
 }

by the way to retrieve data that was saved in your onSaveInstanceState you will override this

 @Override
 protected void onRestoreInstanceState(Bundle savedInstanceState) {
   // TODO Auto-generated method stub
  super.onRestoreInstanceState(savedInstanceState);
}
She Smile GM
  • 1,322
  • 1
  • 11
  • 33
  • i see....well if thats the case then what do i need the onRestoreInstanceState for? if my onCreate can retrieve the data on the app startup? – Ali Elgazar Jan 30 '13 at 04:47
  • I am uncertain if its a good way to retrieve the saved instance from onCreate function, because, onRestoreInstanceState() is used to retrieve what is saved in onSaveInstanceState(). And there are times in which onCreate() is not called instead onRestart(), so you can't retrieve the data since it will skip the onCreate() function. ;) – She Smile GM Jan 30 '13 at 04:54
  • 1
    Well, when the current activity is destroyed which causes the destruction of present state, then onCreate() has to be called everytime, isn't it? That was the reason the developer guide mentions to use either onCreate() or onRestoreInstanceState() to save the state. [Link](http://developer.android.com/guide/topics/resources/runtime-changes.html) – Kanth Jan 30 '13 at 05:07
  • i tried this out and the bundle is always null for some reason, then i tried the onRestoreInstanceState and for some reason it is never called(i tested by creating a toast win the onrestore and showing it) can you explain why to me? – Ali Elgazar Jan 30 '13 at 06:34
  • what do you mean never called? what is not called the onRestoreInstanceState function? – She Smile GM Jan 30 '13 at 06:43
  • i mean it isn't activated .......as in whatever i do inside of it never occurs..... when exactly does the onrestoreinstanestate come in place? when you open the app from the list of apps the first time? or when you exit the app by clicking on the home button then reopening it again? – Ali Elgazar Jan 30 '13 at 06:48
  • It is called after the onSaveInstanceState() is called and when your activity gets restarted. like, for example, when you rotate your device?, it restarted or when you go to other activity and go back again. – She Smile GM Jan 30 '13 at 07:12
1

You have to check for null always in the onCreate() or in onRestoreInstanceState() like below:

String [] b = new String[arraysize];    

   protected void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);      

                  if (savedInstanceState != null)
                  {
                       b = savedInstanceState.getStringArray("MyArray");

                      // Do here for resetting your values which means state before the changes occured.

                  }
                  else{
                          default.. 
                  }

                   Here you do general things.
    }
Kanth
  • 6,681
  • 3
  • 29
  • 41