Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webview reloading on Orientation Change in Honeycomb

I have a webview which is loading a url. If I change the orientation of my tablet the webview load's the URL again. How can I retain the state of webview on orientation change so that it does not restart loading the url. So far I've tried this :-

    @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);
                }
        });
    }
like image 448
Me-an-Droid Avatar asked Dec 17 '22 00:12

Me-an-Droid


1 Answers

In your Manifest file, for the web-view's activity write:

 android:configChanges="orientation|screenSize" 

for newer API's add the screenSizeproperty too...

like image 144
Pallavi Avatar answered Dec 31 '22 12:12

Pallavi