onSaveInstanceState() - 只有一个传递

时间:2018-03-26 11:11:39

标签: java android onsaveinstancestate

更新:包括实际代码

我正在使用ArrayList来填充自定义适配器,它可以在方向更改等方面正常工作:

“的onCreate”

myArrayList = savedInstanceState.getParcelableArrayList(myListkey);

“的onSaveInstanceState”

savedInstanceState.putParcelableArrayList(myListkey, myArrayList);

我可以把它弄好

但是..我需要传递多个变量然后它会中断,看起来它只是我最后插入的那个变量。

实施例1,

“的onCreate”

myNameString = savedInstanceState.getString(namekey);
myArrayList = savedInstanceState.getParcelableArrayList(myListkey);

“的onSaveInstanceState”

savedInstanceState.putString(listkey, myNameString);
savedInstanceState.putParcelableArrayList(myListkey, myArrayList);

此处填充“myArrayList”,但“myNameString”为空

离。 2

“的onCreate”

myArrayList = savedInstanceState.getParcelableArrayList(listkey);
myNameString = savedInstanceState.getString(namekey);

“的onSaveInstanceState”

savedInstanceState.putParcelableArrayList(listkey, myArrayList);
savedInstanceState.putString(namekey, myNameString);

此处填充“myNameString”,但“myArrayList”为空

原始代码:

@Override
protected void onCreate(Bundle savedInstanceState) {

if (savedInstanceState != null) {
_listName = savedInstanceState.getString(getApplicationContext().getResources().getString(R.string.saved_instance_list_name));
_listItems = savedInstanceState.getParcelableArrayList(getApplicationContext().getResources().getString(R.string.saved_instance_list_items_edit));
 }

 ... more code

 }


@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putString(getApplicationContext().getResources().getString(R.string.saved_instance_list_name), _listName);
    savedInstanceState.putParcelableArrayList(getApplicationContext().getResources().getString(R.string.saved_instance_list_items_edit), _listItems);

}

0 个答案:

没有答案