我有一个正在加载 url 的 webview。如果我更改平板电脑的方向,则 webview 加载的 URL 将再次出现。如何在方向更改时保留 webview 的状态,以便它不会重新加载 url。到目前为止,我已经尝试过:-
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
//Save the state of Webview
wv.saveState(savedInstanceState);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore the state of webview
wv.restoreState(savedInstanceState);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager.LayoutParams.FLAG_FULLSCREEN );
setContentView(R.layout.html);
if(savedInstanceState!=null){
// Restore webview state
wv.restoreState(savedInstanceState);
}
WebView wv = (WebView) findViewById(R.id.webView1);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.setPadding(0, 0, 0, 0);
wv.setInitialScale(50);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
_dialog = ProgressDialog.show(HtmlActivity.this, "", "Loading...", true, true);
_dialog.setCancelable(true);
_dialog.show();
wv.loadUrl("http://www.samsung.com");
wv.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return super.shouldOverrideUrlLoading(wv, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//_dialog =ProgressDialog.show(HtmlActivity.this, "", "Please wait...");
super.onPageStarted(wv, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
try{
_dialog.dismiss();
}catch (Exception e) {
// TODO: handle exception
}
// super.onPageFinished(wv, url);
}
});
}