Reputation: 23
I have activity A with a functional WebView1 and 3 buttons. I'm saving the Webview1 state under onPause so once activity B opens the state of WebView1 is already saved. My problem is whenever I open Activity B and try to load WebView1 save state onto WebView11 on activity B it gives me a null pointer (White Screen on phone). How can I save the webview state of activity A so it can be restore in activity B??
Example: Im on activity A webview on the bottom of the page I want to save that state so once I open activity 2 webview it takes me to the bottom of the same page instead of reloading the page.
Main Activity
public class MainActivity extends Activity {
public Bundle webViewBundle;
WebView wv;
Button bt1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wv = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = wv.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(true);
webSettings.setBuiltInZoomControls(true);
wv.loadUrl("http://www.google.com");
wv.setWebViewClient(new myWebViewClient());
addListenerOnbt1();
}
public void addListenerOnbt1() {
// Select a specific button to bundle it with the action you want
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Activity2.class);
Bundle name=webViewBundle;
intent.putExtra("name", name);
startActivity(intent);
finish();
}
});
}
@Override
public void onPause()
{
super.onPause();
webViewBundle = new Bundle();
wv.saveState(webViewBundle);
}
}
Activity2
public class Activity2 extends Activity {
WebView wv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_main);
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
final RelativeLayout rl = new RelativeLayout(this);
wv2 = (WebView) findViewById(R.id.webView11);
WebSettings webSettings = wv2.getSettings();
webSettings.setJavaScriptEnabled(true);
wv2.restoreState(b); // This line returns Null or white screen
}
}
Thank you in advance for your help.
Upvotes: 2
Views: 1990
Reputation: 6409
Try saving the WebView state and starting the next activity on one go.
Bundle state = new Bundle();
webView.saveState(state);
Intent intent = new Intent(MainActivity.this, Activity2.class);
intent.putExtra("state", state);
intent.putExtra("name", name);
And then restoring it from the extra.
webView.restoreState((Bundle)intent.getParcelableExtra("state"));
Upvotes: 5