How to save WebView state and restore it in Android Lollipop?

后端 未结 2 1725
没有蜡笔的小新
没有蜡笔的小新 2021-01-15 05:28

This question is asked multiple times and it has answers that used to work. Recently in documentation said, they have removed this feature for security reasons. Only some li

2条回答
  •  醉话见心
    2021-01-15 05:43

    Setting android:configChanges="orientation|screenSize" for Activity in Manifest for retaining the state of activity that has Webview in it, if there is no difference in layout for portrait and landscape view.

    You can save Webview state as:

    override fun onSaveInstanceState(outState: Bundle) { super.onSaveInstanceState(outState) webView.saveState(outState) 
    } 
    
    override fun onRestoreInstanceState(savedInstanceState: Bundle?) { super.onRestoreInstanceState(savedInstanceState) webView.restoreState(savedInstanceState) 
    }
    

    and reload only if saved state is not null like:

    if (savedInstanceState == null) {
        webView.loadUrl("replace_with_your_url")
    }
    

提交回复
热议问题