我有一个正在加载网址的网页视图。如果我更改平板电脑的方向,则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);
}
});
}
答案 0 :(得分:10)
在您的清单文件中,对于Web视图的活动,请写:
android:configChanges="orientation|screenSize"
对于较新的API,也添加了screenSize
属性...
答案 1 :(得分:1)
除了@Pallavi的答案之外,请阅读http://developer.android.com/guide/topics/resources/runtime-changes.html以了解重新加载的原因。活动重启是Android中活动生命周期的重要组成部分。
答案 2 :(得分:1)
在您的清单文件中,用于网络视图的活动
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"