【问题标题】:How to save state of webview in fragment while detaching?分离时如何在片段中保存webview的状态?
【发布时间】:2020-09-18 10:55:59
【问题描述】:

我有一个包含 webview 的片段。我正在制作该片段的多个实例。

所以每次,我只是分离片段实例,只附加需要的片段实例。

问题是当我分离片段实例并重新附加它时。它只是失去了它的状态,就像 Youtube 在分离之前在一个片段实例中打开一样。重新附加后,它只显示主页(应用主页而不是 Youtube 主页)。

我尝试使用显示和隐藏方法,但它也导致了不同的问题。

片段添加方法

FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragment, new BlankFragment0(), fragemnt tag);// fragment tag is different for every instance
        fragmentTransaction.commit();

我只是根据需要分离和附加片段实例。

【问题讨论】:

    标签: android android-fragments webview android-webview


    【解决方案1】:

    经过几次尝试,我弄明白了

    现在我用onPause方法保存片段的视图。

    public void onPause() {
            super.onPause();
            view = getView();
        }
    

    和 onCreate 方法我检查任一视图是否为空?

    if(view!=null)
            {
                return view;
            }
            else {
                return inflater.inflate(R.layout.fragment_blank0, container, false);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多