4

我有一个正在加载 url 的 webview。如果我更改平板电脑的方向,则 webview 加载的 URL 将再次出现。如何在方向更改时保留 webview 的状态,以便它不会重新加载 url。到目前为止,我已经尝试过:-

    @Override
   public void onSaveInstanceState(Bundle savedInstanceState) {
        //Save the state of Webview
        wv.saveState(savedInstanceState);
        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        // Restore the state of webview
       wv.restoreState(savedInstanceState);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    WindowManager.LayoutParams.FLAG_FULLSCREEN );
    setContentView(R.layout.html);

    if(savedInstanceState!=null){
        // Restore webview state 
        wv.restoreState(savedInstanceState);
     }

    WebView wv = (WebView) findViewById(R.id.webView1);

    wv.getSettings().setJavaScriptEnabled(true);
        wv.getSettings().setBuiltInZoomControls(true);
        wv.setPadding(0, 0, 0, 0);
        wv.setInitialScale(50);
        wv.getSettings().setLoadWithOverviewMode(true);
        wv.getSettings().setUseWideViewPort(true);

        _dialog = ProgressDialog.show(HtmlActivity.this, "", "Loading...", true, true);
        _dialog.setCancelable(true);
        _dialog.show();

        wv.loadUrl("http://www.samsung.com");

        wv.setWebViewClient(new WebViewClient()
        {

                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {

                    return super.shouldOverrideUrlLoading(wv, url);
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                    //_dialog =ProgressDialog.show(HtmlActivity.this, "", "Please wait..."); 
                        super.onPageStarted(wv, url, favicon);
                }

                @Override
                public void onPageFinished(WebView view, String url) {
                    try{
                         _dialog.dismiss();
                        }catch (Exception e) {
                            // TODO: handle exception
                        }
                      //  super.onPageFinished(wv, url);
                }
        });
    }
4

3 回答 3

10

在您的清单文件中,为 web 视图的活动编写:

 android:configChanges="orientation|screenSize" 

对于较新的 API,screenSize也添加属性...

于 2012-05-09T09:42:30.347 回答
1

除了@Pallavi 的回答,请阅读http://developer.android.com/guide/topics/resources/runtime-changes.html以了解为什么会发生重新加载。Activity 重启是 Android 中 Activity 生命周期的重要组成部分。

于 2012-05-09T09:48:09.283 回答
1

在您的清单文件中,用于网络视图的活动

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
于 2015-01-29T10:23:37.857 回答