在 Android WebView 中使用 saveState()
方法可以将当前 WebView 的状态保存到 SharedPreferences 中,以便在下次打开时恢复。
具体实现步骤如下:
WebView webView = findViewById(R.id.web_view);
onSaveInstanceState()
方法,以便在 Activity 或 Fragment 销毁时保存 WebView 状态。@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
webView.saveState(savedInstanceState);
}
saveState()
方法中保存 WebView 状态。public void saveState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("isWebViewLoaded", true);
savedInstanceState.putBoolean("isWebViewSet", true);
savedInstanceState.putBoolean("isWebViewFocused", true);
savedInstanceState.putBoolean("isWebViewVisible", true);
savedInstanceState.putInt("webviewId", webView.getId());
savedInstanceState.putString("url", webView.getUrl());
savedInstanceState.putString("title", webView.getTitle());
savedInstanceState.putString("user_agent", webView.getUserAgentString());
savedInstanceState.putBoolean("isBackKeyEnabled", webView.isBackKeyEnabled());
savedInstanceState.putBoolean("isForwardKeyEnabled", webView.isForwardKeyEnabled());
savedInstanceState.putInt("scrollX", webView.getScrollX());
savedInstanceState.putInt("scrollY", webView.getScrollY());
savedInstanceState.putBoolean("isPageScaleEnabled", webView.isPageScaleEnabled());
savedInstanceState.putBoolean("isTextZoomEnabled", webView.isTextZoomEnabled());
savedInstanceState.putBoolean("isZoomEnabled", webView.isZoomEnabled());
savedInstanceState.putBoolean("isSmoothScrolling", webView.isSmoothScrolling());
savedInstanceState.putBoolean("isScrollEnabled", webView.isScrollEnabled());
savedInstanceState.putBoolean("isLoadWithOverviewMode", webView.isLoadWithOverviewMode());
savedInstanceState.putBoolean("isSupportZoom", webView.isSupportZoom());
savedInstanceState.putBoolean("isDisplayZoomButton", webView.isDisplayZoomButton());
savedInstanceState.putBoolean("isBuiltInZoomControls", webView.isBuiltInZoomControls());
savedInstanceState.putBoolean("isDisplayZoomControls", webView.isDisplayZoomControls());
savedInstanceState.putBoolean("isAlwaysDisplayScrollBars", webView.isAlwaysDisplayScrollBars());
savedInstanceState.putBoolean("isAutoResize", webView.isAutoResize());
savedInstanceState.putBoolean("isAutoSize", webView.isAutoSize());
savedInstanceState.putBoolean("isFitWindow", webView.isFitWindow());
savedInstanceState.putBoolean("isFullScreen", webView.isFullScreen());
savedInstanceState.putBoolean("isShowNavigationBar", webView.isShowNavigationBar());
savedInstanceState.putBoolean("isNavigationBarHidden", webView.isNavigationBarHidden());
savedInstanceState.putBoolean("isScrollBarsEnabled", webView.isScrollBarsEnabled());
savedInstanceState.putBoolean("isFocused", webView.isFocused());
savedInstanceState.putBoolean("isWebViewLoaded", webView.isWebViewLoaded());
savedInstanceState.putBoolean("isFirstLayout", true);
}
onViewCreated()
方法中,从 savedInstanceState 中恢复数据。
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (savedInstanceState != null) {
isWebViewLoaded = savedInstanceState.getBoolean("isWebViewLoaded");
isWebViewSet = savedInstanceState.getBoolean("isWebViewSet");
isWebViewFocused = savedInstanceState.getBoolean("isWebViewFocused");
isWebViewVisible = savedInstanceState.getBoolean("isWebViewVisible");
webViewId = savedInstanceState.getInt("webviewId");
url = savedInstanceState.getString("url");
title = savedInstanceState.getString("title");
userAgent = savedInstanceState.getString("user_agent");
isBackKeyEnabled = savedInstanceState.getBoolean("isBackKeyEnabled");
isForwardKeyEnabled = savedInstanceState.getBoolean("isForwardKeyEnabled");
scrollX = savedInstanceState.getInt("scrollX");
scrollY = savedInstanceState.getInt("scrollY");
isPageScaleEnabled = savedInstanceState.getBoolean("isPageScaleEnabled");
isTextZoomEnabled = savedInstanceState.getBoolean("isTextZoomEnabled");
isZoomEnabled = savedInstanceState.getBoolean("isZoomEnabled");
isSmoothScrolling
领取专属 10元无门槛券
手把手带您无忧上云