5

I'm making my own browser and I need help. I'm trying to save the State of it. I've tried the following things, but not the solution I'm looking for.

I've tried this:

<activity android:name=".About"
    android:configChanges="orientation|keyboard|screenSize" />

but the problem is that the toolbar is not resized, so the title is to small and toolbar height is smallest (if I start in landscape and rotate to portrait) and the title is to big and the heigh bigger (if I start in portrait and rotate to landscape).

The other things I've tried are

@Override
public void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.blah);
   if (savedInstanceState != null)
      ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState.getBundle("webViewState");
}

@Override
protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      Bundle bundle = new Bundle();
      webView.saveState(bundle);
      outState.putBundle("webViewState", bundle);
}

but WebView is not saved correctly.

What can I do to save webview State when rotating and without having problems with toolbar size?

Maybe it's better if I handle config changes and try to resize toolbar and text?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
hegocre
  • 337
  • 3
  • 8

4 Answers4

5

You should save the Webview state into its own Bundle. Also, don't forget to call super.onSaveInstanceState(outState).

@Override
public void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.blah);
   if (savedInstanceState != null)
      ((WebView)findViewById(R.id.webview)).restoreState(savedInstanceState.getBundle("webViewState"));
}

@Override
protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);
      Bundle bundle = new Bundle();
      webView.saveState(bundle);
      outState.putBundle("webViewState", bundle);
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • 1
    This doesn't save Javascript variables or changes to the DOM made by Javascript. – Donald Duck Aug 14 '21 at 17:35
  • This is unfortunately true. I'm not sure what you can do for that without a Javascript interface and manually passing those variables out of the WebView. – EpicPandaForce Aug 14 '21 at 22:22
  • Can this answer be optimized with the help of `onRestoreInstanceState()`? – G. Ciardini Nov 18 '22 at 11:32
  • According to this [docs](https://developer.android.com/guide/components/activities/activity-lifecycle#oncreate), `super.onSaveInstanceState(outState);` should be called after the `outState` is being modified. – G. Ciardini Nov 18 '22 at 11:35
4

First step

<activity android:name=".MainActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize">

Second step

 if (savedInstanceState != null)
    {  ((WebView) findViewById(R.id.webView)).restoreState(savedInstanceState.getBundle("webViewState"));}
    else {
       wv1=(WebView)findViewById(R.id.webView);
        load(); // create seprate functin to loadweb
    }

Third Step

  public void load() {
    wv1=(WebView)findViewById(R.id.webView);
    wv1.setWebViewClient(new MyBrowser());
    wv1.getSettings().setLoadsImagesAutomatically(true);
    wv1.getSettings().setJavaScriptEnabled(true);
    wv1.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    wv1.loadUrl("http://citynsr.ueuo.com/");
}
private class MyBrowser extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

Forth Step

 protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Bundle bundle = new Bundle();
    wv1.saveState(bundle);
    outState.putBundle("webViewState", bundle);
}
@Override
protected void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    Bundle bundle = new Bundle();
    wv1.saveState(bundle);
    state.putBundle("webViewState", bundle);
}
1

I am using

<activity
    android:name="XYZ.activity.your.browser.activity"
    android:configChanges="orientation|screenSize|keyboardHidden|keyboard"
    android:windowSoftInputMode="adjustPan"/>

inside my AndroidManifest.xml file which works just fine. No resizing issues.

I would not recommend this in regular activities, however if you only have a WebView, a toolbar and a few buttons then this should be fine.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Zun
  • 1,553
  • 3
  • 15
  • 26
  • Yes, but if I start activity in landscape mode and I rotate to portrait, toolbar title is too small, and toolbar has a smallest height than if it starts in portrait. – hegocre Aug 22 '16 at 20:34
1

Improving @EpicPandaForce Answer and it works!

WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState != null)
        ((WebView) findViewById(R.id.webView)).restoreState(savedInstanceState.getBundle("webViewState"));
    else {
        webView = (WebView) findViewById(R.id.webView);
        webView.loadUrl("https://www.YAHOO.com/");
    }

}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    Bundle bundle = new Bundle();
    webView.saveState(bundle);
    outState.putBundle("webViewState", bundle);
}
Manuel Allenspach
  • 12,467
  • 14
  • 54
  • 76