如何保存&恢复Webview状态?

时间:2013-08-28 04:33:20

标签: android webview

当我使用备份&恢复Webview状态,我收到此消息: x地址处的网页可能是临时性的,或者可能已永久移动到新的网址。

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    webViewShowPoll.saveState(savedInstanceState);                                    
}

@Override
public void onRestoreInstanceState(Bundle outState) {
  super.onRestoreInstanceState(outState);
    webViewShowPoll.restoreState(outState);
}

和Androidmanifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_DEBUG_APP"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />    
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    


    <activity
        android:name="com.omid.epoll.mobile.Poll"
        android:launchMode="singleInstance"
        android:label="@string/title_activity_poll" 
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
    </activity>

4 个答案:

答案 0 :(得分:3)

restoreState永远不可靠。这也许就是文档现在说的原因。

  

如果在此WebView有机会构建状态后调用它   (可能有加载页面,创建后退/前进列表等)   不良副作用。请注意,这种方法不再   恢复此WebView的显示数据。

因此saveState()的相应条目说明了:

  

请注意,此方法不再存储显示数据   这个WebView。

你在onCreate方法中真正应该做的是调用webView.loadUrl()如果你想显示上次访问的网址,请看answer

如果您担心在方向更改等方面重新加载webview

  

您可以将Activity设置为处理方向和keyboardHidden更改,然后单独保留WebView

答案 1 :(得分:2)

 if (isInternetPresent) {
        // Internet Connection is Present
        // make HTTP requests
        // showAlertDialog(HomeScreen.this, "Internet Connection",
        // "You have internet connection", true);

        webviewbrowse.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webviewAds.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
        webviewbrowse.loadUrl("http://www.example.com");
    } else {
        // Internet connection is not present
        // Ask user to connect to Internet
        webviewbrowse.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webviewAds.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        webviewbrowse.loadUrl("http://example.com");
        showAlertDialog(HomeScreen.this, "internet doesn't connect",
                " please connect to internet", false);
    }

答案 2 :(得分:1)

     binding.webView.setWebViewClient(new WebViewClient() {
         public boolean shouldOverrideUrlLoading(WebView view, String url) {
             view.loadUrl(url);
             return true;
         }

         @Override
         public void onPageStarted(WebView view, String url, Bitmap favicon) {
             super.onPageStarted(view, url, favicon);
         }

         @Override
         public void onPageFinished(WebView view, String url) {
             super.onPageFinished(view, url);
         }
     }
    );

    binding.webView.getSettings().setJavaScriptEnabled(true);
    binding.webView.getSettings().setBuiltInZoomControls(true);
    binding.webView.getSettings().setDisplayZoomControls(false);
    binding.webView.getSettings().setAppCacheMaxSize( 5 * 1024 * 1024 );
    binding.webView.getSettings().setAppCachePath( getActivity().getApplicationContext().getCacheDir().getAbsolutePath() );
    binding.webView.getSettings().setAllowFileAccess( true );
    binding.webView.getSettings().setAppCacheEnabled( true );
    binding.webView.getSettings().setCacheMode( WebSettings.LOAD_DEFAULT );
    if ( !ConnectivityStatus.Companion.isConnected(getContext()) ) { // loading offline
        binding.webView.getSettings().setCacheMode( WebSettings.LOAD_CACHE_ELSE_NETWORK );
    }
    binding.webView.loadUrl(this.url);

答案 3 :(得分:0)

如果您的WebView位于片段中,请查看以下内容:

省电:

@Override
public void onSaveInstanceState(Bundle outState) {
    webView.saveState(outState); // output would be a WebBackForwardList
}

You may check the doc for saveState.

恢复:

@Override
public void onCreate(Bundle savedInstanceState) {
    ...

    if (savedInstanceState != null) {
        webView.saveState(savedInstanceState);
    } else {
        webView.loadUrl("http://mypage");
    }
}