Webview updating URL page when I turn the screen

2

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

Any tips?

    
asked by anonymous 22.07.2017 / 14:27

2 answers

0

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

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

And in Manifest , in Webview Activity add also

android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    
22.07.2017 / 14:45
0

This happens because when the device is run to Activity where the WebView is destroyed and recreated, causing the oncreate() method to be called.

Override method onSaveInstanceState and save the URL that WebView is displaying.

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

It will be anything like this:

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());

}
    
22.07.2017 / 14:59