首页 > 解决方案 > 如何恢复 webview 状态

问题描述

我正在像这样保存我的状态:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    binding.webView.restoreState(savedInstanceState)
}

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    binding.webView.saveState(outState)
}

创建时

shouldRestoreState = savedInstanceState?.let {
    true
} ?: false
bundle = savedInstanceState

OnViewCreated

    if (shouldRestoreState && bundle != null) {binding.webView.restoreState(bundle)} else { load normally }

它确实有效

A.几次屏幕旋转后,屏幕变黑几秒钟,然后加载显示页面内容

B.当我在 youtube 页面中旋转屏幕时,它会加载屏幕但没有任何内容。

我的代码做错了什么,我该如何解决?

标签: androidkotlinandroid-webview

解决方案


推荐阅读