因此,我有一个EditText,我在其上设置了一个EditoractionListener,即在用户输入文本并按enter/search后,它将获取详细信息并相应地填充回收器视图。
现在为了保存配置更改的状态,我编写了以下代码-
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);
}
}
我正在使用MVP,所以我正在演示者的回调中设置适配器。
在屏幕旋转后,当我们在键盘上单击enter/search时,我能够保持状态,所以我在onRestoreInstanceState()中尝试了这种黑客攻击,但我认为应该有更好的方法来实现这一点。
@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);
}
}
提前谢谢。