Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

saving ArrayList in the bundle savedInstanceState

Tags:

android

the ArrayList is defined on class level. these are my savedInstance methods:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putStringArrayList("savedList", list);
}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    list=savedInstanceState.getStringArrayList("savedList");
}

but still, when i change orientation the ArrayList is blank

like image 416
user2329454 Avatar asked Apr 28 '13 16:04

user2329454


People also ask

How do I save an ArrayList in onSaveInstanceState?

You should simply implement the Parcelable class for your object and use Bundle#putParcelableArrayList() in onSaveInstanceState() and onRestoreInstanceState() . This method will store an ArrayList of Parcelables by itself.

What is onCreate bundle savedInstanceState in Android?

What is the savedInstanceState Bundle? The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

How do you save onSaveInstanceState?

1 Answer. Show activity on this post. You can replace above with the data you want to save and then re-populate your views like RecyclerView etc with the data you saved in the savedInstance as key-value pair. Just keep in mind that the Bundle is not meant to store Large amount of data.

What is bundle onCreate?

onCreate(Bundle savedInstanceState) Function in Android: Basically Bundle class is used to stored the data of activity whenever above condition occur in app. onCreate() is not required for apps. But the reason it is used in app is because that method is the best place to put initialization code.


2 Answers

When you use onRestoreInstanceState() to restore state, its called after onStart() so you update your list with the saved state after you define your adapter. Your best option is to restore the list in onCreate() the same way you do it on onRestoreInstanceState(). You can also redefine the adapter or call notifyDataSetChanged().

like image 170
Aviel Gross Avatar answered Oct 26 '22 11:10

Aviel Gross


TextView textviewname;
String textname;

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
          setContentView(R.layout.activity_main);

       //declare the ui like this
    textviewname = (TextView) findViewById(R.id.textviewid);
       if(savedInstanceState==null){
       //the first time the activity was created
       //do the normal flow of the code
       //Example:getting the intent data for(savedList)or processing it
       Intent i = getIntent();
       textname = i.getStringExtra("savetext");
      //or textname="name_data";
       }else{
       //this is called when orientation change occur
       //get the savedata
        list=savedInstanceState.getStringArrayList("savedList");
        textname=savedInstanceState.getString("savetext");
       }

       textviewname.setText(textname);

}   
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //saving data when orientation change trigger
    //saving  data before ondestroy happens
//onSaveInstanceState is called before going to another activity or orientation change
    outState.putStringArrayList("savedList", list);
    outState.putString("savetext", textname);
    //all imnportant data saved and can be restored
}


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //this is  only called after ondestroy-> oncreate occur
    list=savedInstanceState.getStringArrayList("savedList");
    textname=savedInstanceState.getString("savetext");
    //oncreate->onSaveInstanceState->ondestroy->oncreate->onRestoreInstanceState
}
like image 36
Cristiana Chavez Avatar answered Oct 26 '22 12:10

Cristiana Chavez