问题
I have 3 fragments in my App.1 fragment is having webview.
I am using recyclerview.Using onclick method to switch between fragments.
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment, fragment);
fragmentTransaction.commit();
I tried below code to save state of fragment
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
webView.saveState(outState);
}
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webView.restoreState(savedInstanceState);
}
It is usefull to save state for orientation change.
I also tried
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
But when I switch between fragment it doesn't restore the state.
UPDATE
Using Viewpager and pageadapter helps me in solving this problem.
来源:https://stackoverflow.com/questions/62025123/problem-in-saving-state-of-webview-inside-fragment