🎰Maintain Stack in WebView — Android

Maintain your stacks in WebView Easily

Danish Amjad
AndroidPub

--

Well, In Android Applications we often used WebViews and for that, we simply add the WebView in our XML layout file and just load the URL in our WebView, like below snippet.

WebView.xml

Now, we have multiple pages Web or we have a single page Web, the problem is how we gonna maintain the stack in WebView, If users go from one page to another page & by pressing the back button hardware/ toolbar back button, then we need to redirect to the previously visited page.

Visit my website at https://danishamjad.com/

The Best thing is that WebView gives us the Information that the page can go back or not with this function:

webview.canGoBack()

In Android, we have the hardware back button and we have also the toolbar button so how you gonna achieve this.

Just make a general function like below:

private fun checkIfPageCanGoBack(): Boolean {
return webview.canGoBack()
}

--

--