在Fragment中保存状态WebView

时间:2013-11-21 15:16:38

标签: android android-fragments webview

我想要在Fragment中保存状态的webview。我想要的是。当我通过导航抽屉切换片段时,我想在片段中保存我的webview状态。我找到了一些代码来保存状态,但实际上我不知道在哪里写它。有人可以帮我吗,拜托?我很抱歉我的英语..

这是savestate的代码

@Override
public void onSaveInstanceState(Bundle outState) {
   super.onSaveInstanceState(outState);
   mWebView.saveState(outState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
   super.onActivityCreated(savedInstanceState);
   mWebView.restoreState(savedInstanceState);
}

这是我的代码

public class Zastupovanie extends SherlockFragment{




    Context context;



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
            {
            View v = inflater.inflate(R.layout.zastupovanie, group, false);
            final ProgressDialog pd = ProgressDialog.show(getActivity(),  "", "Prosím čakajte. Prebieha načítavanie... ", true);

            final WebView myWebView = (WebView) v.findViewById(R.id.zastupovanie2);
            myWebView.setWebViewClient(new WebViewClient() {
                @Override
                public void onPageFinished(WebView view, String url) {
                    pd.dismiss();
                }
            });

            myWebView.getSettings().setBuiltInZoomControls(false); 
            myWebView.getSettings().setSupportZoom(false);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setAllowFileAccess(true); 
            myWebView.getSettings().setDomStorageEnabled(true);

            myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);

            myWebView.loadUrl("http://soszm.edupage.org/substitution/?");

            return v;
            }

        }

2 个答案:

答案 0 :(得分:1)

您可以将此方法简单地添加到您的片段中,如下所示:

public class Zastupovanie extends SherlockFragment{

    Context context;
    private WebView myWebView

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        myWebView.saveState(outState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        myWebView.restoreState(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved)
        {
        View v = inflater.inflate(R.layout.zastupovanie, group, false);
        final ProgressDialog pd = ProgressDialog.show(getActivity(),  "", "Prosím čakajte. Prebieha načítavanie... ", true);

        myWebView = (WebView) v.findViewById(R.id.zastupovanie2);
        myWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                pd.dismiss();
            }
        });

        myWebView.getSettings().setBuiltInZoomControls(false); 
        myWebView.getSettings().setSupportZoom(false);
        myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);   
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.getSettings().setAllowFileAccess(true); 
        myWebView.getSettings().setDomStorageEnabled(true);

        myWebView.getSettings().setDefaultZoom(ZoomDensity.FAR);

        myWebView.loadUrl("http://soszm.edupage.org/substitution/?");

        return v;
        }

    }

答案 1 :(得分:0)

BottomNavigationView中的Webview

我正在BottomViewNavigation的其中一个片段中使用Web视图。 @Rafael T提供的解决方案是可靠的。但是,当您导航到BottomNavigationView中的另一个片段时,不会调用onSaveInstanceState()。所以我这样保存和恢复了Webview的状态。

class MyFragment extends Fragment {

    private Bundle webviewstate;
    private Webview mywebview;

    // Saving state
    @Override
    public void onPause(){
       super.onPause();
       webviewstate = new Bundle();
       mywebview.saveState(webviewstate);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){

       if(webviewstate==null){
            myWebView.loadUrl("http://soszm.edupage.org/substitution/?");  //Load the page for first time
       }else{
            myWebView.restoreState(webviewstate);    // Restore the state
       }

    }

}