2

This is my first app, it is a web app, it simply loads a webView to a url where stuff happens.

It works fine in android browser(gingerbread), but in Chrome(ICS, JB) it was going back to the initial webView url when the device is rotated. In gingerbread you account for this by setting android:configChanges="keyboard|keyboardHidden|orientation" and overriding onConfigurationChanged.

After searching the web, I re-wrote my original activity as per the example here: http://www.devahead.com/blog/2012/01/preserving-the-state-of-an-android-webview-on-screen-orientation-change/ , in which you essentially handle the re-orientation yourself.

However now my app is crashing when I rotate the device.

Testing with Android 4.1.2.

Anyone see anything off with my code? Sorry if the answer is obvious, but I've been stuck on this bug for a couple of days now. Maybe I just need another set of eyes on it. Thanks in advance!

Note: the line @SuppressWarnings("deprecation") is not in the example, it was suggested by the editor in order to compile.

The Main Activity:

package com.example.xxx.xxx;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.Menu;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

@SuppressLint("SetJavaScriptEnabled")
public class MainActivity extends Activity {

protected FrameLayout webViewPlaceholder;
protected WebView myWebView;

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

    initUI();

}

   @SuppressWarnings("deprecation")
   protected void initUI() {

    webViewPlaceholder = ((FrameLayout)findViewById(R.id.webViewPlaceholder));


    if (myWebView == null){
        //Create It
        myWebView = new WebView(this);
        myWebView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setLoadsImagesAutomatically(true);
        myWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
        myWebView.setScrollbarFadingEnabled(true);
        myWebView.setWebViewClient(new WebViewClient());
        myWebView.setWebChromeClient(new WebChromeClient());
        myWebView.setInitialScale(1);
        myWebView.loadUrl("http://www.theurl.com");

    }

    webViewPlaceholder.addView(myWebView);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onConfigurationChanged(Configuration newConfig){   

    if (myWebView != null) {

        webViewPlaceholder.removeView(myWebView);

    }

    super.onConfigurationChanged(newConfig);

    setContentView(R.layout.activity_main);

    initUI();

}


@Override
protected void onSaveInstanceState(Bundle outState)
      {
        super.onSaveInstanceState(outState);

        // Save the state of the WebView
        myWebView.saveState(outState);
      }


@Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
      {
        super.onRestoreInstanceState(savedInstanceState);

        // Restore the state of the WebView
        myWebView.restoreState(savedInstanceState);
      }
}

Main Activity XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="horizontal" >

<FrameLayout android:id="@+id/webViewPlaceholder"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>

</LinearLayout>
zsmith_stl
  • 41
  • 3
  • I just updated to Android 4.2 minutes after this post, and the saving/loading of states is now working. Now I need to figure out how to keep my form data intact. Any ideas? – zsmith_stl Nov 14 '12 at 20:43

2 Answers2

1

try adding orientation to you activity definition in manifest file as follows:

<activity android:screenOrientation="portrait"  ...>
</activity>
Praful Bhatnagar
  • 7,425
  • 2
  • 36
  • 44
  • Thanks for the reply. I gave your suggestion a try, but it just locks the orientation. It is one workaround to this, but I would like to allow user rotation and still retain any form data they've entered thus far. edit: sorry I don't have enough rep to up vote. – zsmith_stl Nov 14 '12 at 22:52
0

I realized I don't even need WebChromeClient at all if I'm not using any of it's features. Found answer here: How to prevent WebView auto refresh when screen rotation

simply add "screenSize" to your configchanges in manifest.

android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
Community
  • 1
  • 1
zsmith_stl
  • 41
  • 3