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(stat开发者_开发技巧e);
//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();
}
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
.
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.
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.
精彩评论