简体   繁体   English

在bundle savedInstanceState中保存ArrayList

[英]saving ArrayList in the bundle savedInstanceState

the ArrayList is defined on class level. ArrayList是在类级别定义的。 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 但是,当我改变方向时,ArrayList是空白的

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. 当您使用onRestoreInstanceState()恢复状态时,它会在onStart()之后onStart()因此您在定义适配器后使用保存的状态更新列表。 Your best option is to restore the list in onCreate() the same way you do it on onRestoreInstanceState() . 您最好的选择是恢复onCreate()的列表,就像在onRestoreInstanceState() You can also redefine the adapter or call notifyDataSetChanged() . 您还可以重新定义适配器或调用notifyDataSetChanged()

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
}

try to use 试着用

list.addAll(savedInstanceState.getStringArrayList("savedList"));

instead 代替

list=savedInstanceState.getStringArrayList("savedList");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM