Refresh in Webview when rotating device

Asked

Viewed 500 times

6

I created my application via WebView, but when I rotate the screen the site gives refresh, or is back to the 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"/>

1 answer

7


By default, when turning the phone the method onCreate is called again and with this, data that was dynamically loaded will be lost unless you do a treatment saving the state using the method onSaveInstanceState() in his Activity. See below:

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

To restore the state in your WebView just sweat the method restoreState(). 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/");
}

It’s pretty much the same thing keep data from a list while rotating on Android, but using WebView.

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

To log on, I created a gist file adapted to its code.

  • I’m very new, in which case I copy these scripts and replace mine?

  • @Evertongouveia edited the question and insert a link with a file to adapt to your code. See: https://gist.github.com/cleidimarviana/c269a626f92cefa15c262a6de271d62b

  • would you be able to block the rotation? with my code?

  • @Evertongouveia has yes: https://answall.com/a/99115/35406

  • @Evertongouveia managed to solve?! Any more doubts?

  • I’m trying here #Vdc

  • 1

    It’s already worked out, see, I put a rotation blocker.

  • @Evertongouveia Cool! But then do a search later! A while ago google was kind of filtering out the locked screen apps to get further down the play store search list.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.