When using WebViews to display HTML content, and using a new Activity to show a new page, the state of all WebViews is automatically preserved and restored by the OS when a new WebView activity is opened/closed. This leads to an uninterrupted back-navigation user experience.
Having said that, I'm wondering if it's possible to replicate such experience when using Jetpack Compose Navigation or any of the 3rd party libraries that wrap around it to provide high-level navigation API? If I understand correctly, the issue at hand is that under a single-activity architecture Composables are completely disposed of and recreated. We need to keep the persistent state in a lifecycle aware component, such as ViewModel. Trouble is, I couldn't find a way to do that with a WebView, even with the one provided by the Accompanist library. The lack of complete state restoration leads to the loss of currently loaded page when navigating back, as well as the scroll position, thus greately hurting the user experience. I tried experiementing with saveState
and restoreState
methods of the WebView
class, to no avail. Hence I would greatly appreciate if someone can show a working example of how one can fully save and restore the state of the WebView upon back-navigation when using the Compose navigation APIs.