Problem in Saving State of Webview inside Fragment

早过忘川 提交于 2020-06-17 09:18:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!