Refresh in WebView when rotating device

6

I created my application via WebView , but when I rotate the screen the site gives refresh , that is back to login , my code:

MainActivity:

    package com.sirseni.simpleandroidwebviewexample;
    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends Activity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            WebView myWebView = (WebView) findViewById(R.id.myWebView);
            myWebView.loadUrl("http://meulink.com/");

            myWebView.setWebViewClient(new MyWebViewClient() {
                @Override
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return false;
                }

            });

            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);

        }

        // Use When the user clicks a link from a web page in your WebView
        private class MyWebViewClient extends WebViewClient {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (Uri.parse(url).getHost().equals("http://meulink.com/")) {
                    return false;

                }

                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        }
    }

Main_Activity:

<WebView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myWebView"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    
asked by anonymous 05.06.2017 / 18:21

1 answer

7

By default, when you rotate the phone, the onCreate method is called again and thus, data that was dynamically loaded will be lost unless you treat it by saving the state using the onSaveInstanceState() method on your Activity . See below:

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

To restore the state to its WebView , simply sweat the restoreState() method. See below:

WebView myWebView;

@Override
public void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   myWebView = (WebView)findViewById(R.id.myWebView);
   // aqui verifica que o estado está diferente de null. 
   if (savedInstanceState != null)
      myWebView.restoreState(savedInstanceState);
   else
      myWebView.loadUrl("http://meulink.com/");
}

Basically it's almost the same as keep data from a list when rotating on Android , however using WebView .

You can read more about the how to recreate an activity and the life cycle of an activity .

To register, I've created a Gist file tailored to your code.

    
05.06.2017 / 18:35