Webview updating URL page when rotating the screen

Asked

Viewed 536 times

3

I’m running a test on the webview and it’s ok. But when I turn the smartphone it updates my page url. I would like to keep the rotation without this update, keeping it on the screen that the url is.

Any hint?

2 answers

0

Well I got it here. Anyone who has the same problem in the webview XML, add

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"

And in the Manifest , in the Webview Activity add also

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
  • But in this case, when rotating the device, the screen does not rotate.

  • @ramaral here spun on smartphone. I tested on direct physical

  • 2

    You’re right. What I meant is that if you have layouts for Landscape and Portrait, Resources by language and other types of configuration they are not automatically applied. This approach solves this problem but creates many others.

0

This is because when the device runs Activity where Webview is destroyed and recreated, making the method oncreate() be called.

Do the override of the method onSaveInstanceState and save the Url that Webview is showing.

Later, when Activity is recreated, you can get the Url of Bundle passed to the method onCreate().

It’ll be something like:

private String mUrl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Verifica se a Activity foi recreada.
    if (savedInstanceState != null) {
        // Recupera a Url
        mUrl = savedInstanceState.getString("Url");
    } else {
        mUrl = "valor default da url";
    }
    ...
}

@Override
public void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putString("Url", suaWebView.getUrl());

}

Browser other questions tagged

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