8

I have a simple webView app which loads a page from our site with locations and phone numbers that our field guys are supposed to visit each day. It provides a hook to launch navigation or dial the phone. When returning from the outside activity, the app is crashing. Hitting the phone's home button then dialing the phone, then returning to the app works fine.

here's the code...

Update 2 - No longer crashing, webview is blank upon returning

public class VSIMobile_WebView extends Activity {
private static String PROVIDER="gps";
private WebView browser;
private LocationManager myLocationManager=null;
private TelephonyManager myTeleManager = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    browser=(WebView)findViewById(R.id.webview);
    myLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
    myTeleManager=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    browser.getSettings().setJavaScriptEnabled(true);
    browser.addJavascriptInterface(new Locater(), "locater");
    browser.addJavascriptInterface(new JavaScriptInterface(this), "Android");
    if (savedInstanceState!=null){
        browser.restoreState(savedInstanceState);
    }else{ 
        browser.loadUrl("http://zzz.com");
        browser.setWebViewClient(new HelloWebViewClient());
    }
}

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    }

@Override
public void onSaveInstanceState(Bundle outState) {
    //Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
    browser.saveState(outState);
} 

@Override
protected void onRestoreInstanceState(Bundle state) {
    browser.restoreState(state);
    super.onRestoreInstanceState(state);
    //Toast.makeText(getBaseContext(), browser.getUrl(), Toast.LENGTH_LONG).show();
}


/* Did not work
public void onResume(Bundle inState){
    browser.restoreState(inState);
    super.onResume();   
}
public void onPause(Bundle outState) {
    browser.saveState(outState);
    super.onPause();
}
*/


@Override
public void onResume(){
    super.onResume();   
}


@Override
public void onPause() {
    super.onPause();
}
Alex K
  • 8,269
  • 9
  • 39
  • 57
RNeumann
  • 81
  • 1
  • 3

3 Answers3

0

Upon resuming your app, apply locationManager.getLastKnownLocation to get the last stored location and inject it inside your webview's javascript. You should do this only if the locationManager is null. And locationManager drains battery, it would be wise not to use it when the user is not actively using the app.

user3486470
  • 296
  • 1
  • 11
Bijay Koirala
  • 525
  • 5
  • 9
0

Enabling the following properties,in addition to the above code works like a charm :)

android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"

for each activity containing the WebView in AndroidManifest.xml worked for me. Although it is a generalized answer, would solve your and many other such people stuck with this problem.

0

Shouldn't you be setting your myLocationManager and other instance variables even if savedInstanceState != null?

Restoring the WebView state isn't going to do this for you, and your app is probably crashing when it tries to derefence one of them, giving you a NullPointerException.

Matthew
  • 44,826
  • 10
  • 98
  • 87
  • Able to return to the app now without crashing, but just getting a blank webbview with the application title. Also, can't reproduce in the emulator. – RNeumann Mar 27 '11 at 21:27
  • On top of everything else, I somehow managed to screw up eclipse on my machine and now can not open the emulator. Going through a re-install this morning. – RNeumann Mar 28 '11 at 12:44