Fragment的已恢复instanceState包始终为null

时间:2015-10-28 22:55:57

标签: android android-fragments android-recyclerview

我正在尝试保存Recyclerview的LinearLayoutManager状态。

Fragment.java:

@Override
    public void onSaveInstanceState(Bundle outState) {
        Log.i("frag", "onSaveInstanceState called");
        super.onSaveInstanceState(outState);
        outState.putParcelable("myState", mGridLayoutManager.onSaveInstanceState());
    }

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
          if (savedInstanceState != null) {
                Log.i(MainActivity.TAG, "restore called from createView");
                mGridLayoutManager.onRestoreInstanceState(savedInstanceState.getParcelable("myState"));
            }
          .....
          ....
    }

savedInstanceState始终为空。

我已经尝试在 onCreate &中恢复其状态 onActivityCreated 方法,但徒劳无功。我做错了什么?

我检查了这些问题,以确保这是正确的保存方式。恢复:

Recyclerview store / save state

Save recyclerview scroll position

1 个答案:

答案 0 :(得分:0)

在调用outstate.putParcelable()后尝试调用super.onSaveInstanceState(outstate)?

如果这不起作用,可以尝试在字符串中放一个字符串,看看它是否与mGridLayoutManager.onSaveInstanceState()失败有关?