首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Android webView saveState

在 Android WebView 中使用 saveState() 方法可以将当前 WebView 的状态保存到 SharedPreferences 中,以便在下次打开时恢复。

具体实现步骤如下:

  1. 在 WebView 中获取 WebView 对象的引用。
代码语言:java
复制
WebView webView = findViewById(R.id.web_view);
  1. 在 Activity 或 Fragment 中定义一个 onSaveInstanceState() 方法,以便在 Activity 或 Fragment 销毁时保存 WebView 状态。
代码语言:java
复制
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    webView.saveState(savedInstanceState);
}
  1. saveState() 方法中保存 WebView 状态。
代码语言:java
复制
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);
}
  1. onViewCreated() 方法中,从 savedInstanceState 中恢复数据。
代码语言:java
复制

@Override

public void onViewCreated(View view, Bundle savedInstanceState) {

代码语言:txt
复制
super.onViewCreated(view, savedInstanceState);
代码语言:txt
复制
if (savedInstanceState != null) {
代码语言:txt
复制
    isWebViewLoaded = savedInstanceState.getBoolean("isWebViewLoaded");
代码语言:txt
复制
    isWebViewSet = savedInstanceState.getBoolean("isWebViewSet");
代码语言:txt
复制
    isWebViewFocused = savedInstanceState.getBoolean("isWebViewFocused");
代码语言:txt
复制
    isWebViewVisible = savedInstanceState.getBoolean("isWebViewVisible");
代码语言:txt
复制
    webViewId = savedInstanceState.getInt("webviewId");
代码语言:txt
复制
    url = savedInstanceState.getString("url");
代码语言:txt
复制
    title = savedInstanceState.getString("title");
代码语言:txt
复制
    userAgent = savedInstanceState.getString("user_agent");
代码语言:txt
复制
    isBackKeyEnabled = savedInstanceState.getBoolean("isBackKeyEnabled");
代码语言:txt
复制
    isForwardKeyEnabled = savedInstanceState.getBoolean("isForwardKeyEnabled");
代码语言:txt
复制
    scrollX = savedInstanceState.getInt("scrollX");
代码语言:txt
复制
    scrollY = savedInstanceState.getInt("scrollY");
代码语言:txt
复制
    isPageScaleEnabled = savedInstanceState.getBoolean("isPageScaleEnabled");
代码语言:txt
复制
    isTextZoomEnabled = savedInstanceState.getBoolean("isTextZoomEnabled");
代码语言:txt
复制
    isZoomEnabled = savedInstanceState.getBoolean("isZoomEnabled");
代码语言:txt
复制
    isSmoothScrolling
页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券