因此,我有一个 EditText,我在其上设置了 onEditorActionListener,即在用户输入文本并按下回车/搜索后,它将获取详细信息并相应地填充回收站视图。
现在为了保存配置更改的状态,我编写了以下代码 -
Parcelable stateList;
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Saving instance of the state in Parcelable stateList
stateList = recyclerView.getLayoutManager().onSaveInstanceState();
outState.putParcelable(RETAIN_STATE, stateList);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState!=null) {
stateList = savedInstanceState.getParcelable(RETAIN_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(stateList);
}
}
但是当我运行它并旋转屏幕时,回收器视图不会从 stateList parcelable 恢复状态。
我正在使用 MVP,所以我在演示者的回调中设置了适配器。
当我们在屏幕旋转后单击键盘上的输入/搜索时,我能够保留状态,所以我在 onRestoreInstanceState() 中尝试了这个 hack,但我认为应该有更好的方法来解决这个问题。
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState!=null) {
//The hack!
et_search.onEditorAction(EditorInfo.IME_ACTION_SEARCH);
stateList = savedInstanceState.getParcelable(RETAIN_STATE);
recyclerView.getLayoutManager().onRestoreInstanceState(stateList);
}
}
让我知道是否需要更多信息。提前致谢。