Skip to content
webtoapkfree

How do I make the back button go to the previous page in an Android WebView app?

Fix the WebView back button with an OnBackPressedCallback that calls goBack() while canGoBack() is true. Old onBackPressed code breaks on Android 16.

GuidesNookesh Karri

Light cover card with the title 'How do I make the back button go to the previous page in an Android WebView app?' in dark text and a green accent bar

To make the WebView back button go to the previous page, register an OnBackPressedCallback that calls webView.goBack(), and keep it enabled only while webView.canGoBack() is true. When there’s no history left, the callback turns off and the system closes the screen as usual. The old fix, overriding onBackPressed() or onKeyDown(), no longer runs in apps that target Android 16.

Why the back button closes a WebView app

A WebView keeps its own page history, but Android’s back button doesn’t know about it. Pressing back finishes the activity, so a user three pages deep in your site lands on the home screen.

Android’s WebView guide names the two methods you need: “You can navigate backward and forward through the history with goBack and goForward.” And “the canGoBack method returns true if there is web page history for the user to visit.”

History only builds up when the WebView loads the pages itself: “when your WebView overrides URL loading, it automatically accumulates a history of visited web pages.” Send links to an external browser and there’s nothing to go back to.

The WebView back button code that works today

The current way to handle the WebView back button is an OnBackPressedCallback registered on the activity’s back dispatcher. It needs AndroidX Activity 1.6.0 or higher, which the predictive back guide names as the backward compatible API.

This is what I use in an activity, in Kotlin:

val backToPreviousPage = onBackPressedDispatcher.addCallback(this, enabled = false) {
    webView.goBack()
}

webView.webViewClient = object : WebViewClient() {
    override fun doUpdateVisitedHistory(view: WebView, url: String?, isReload: Boolean) {
        backToPreviousPage.isEnabled = view.canGoBack()
    }
}

It starts disabled because the first page has no history. Each time the WebView records a visit, doUpdateVisitedHistory flips it to match canGoBack().

Google’s predictive back codelab does the same. Its goal for WebView is to “use the Back gesture to go back in the page stack until there are no more pages.”

Why old onBackPressed snippets stop working

Old snippets that override onBackPressed() or catch KeyEvent.KEYCODE_BACK stop working once your app targets Android 16. Most answers online skip this.

From Android 16’s behavior changes: for apps targeting API level 36 on Android 16 devices, “onBackPressed is not called and KeyEvent.KEYCODE_BACK is not dispatched anymore.” Predictive back animations are on by default.

Google Play requires new apps and updates to target Android 16 (API level 36) or higher since August 31, 2026. Raise your target to ship an update and a copied Stack Overflow fix breaks without an error. The app builds. Back just closes it again. I explain the target rule in Google Play’s target API level requirement.

Even the WebView guide’s first sample overrides onKeyDown. Read the note under it: “If your app uses Jetpack AppCompat 1.6.0+, you can simplify the previous snippet” with onBackPressedDispatcher.addCallback. That callback should be your default.

Exiting the app when there’s no history left

Exiting cleanly means giving the back gesture back to the system once the WebView is on its first page. The codelab puts it plainly: “Your app needs to return the Back navigation to the system once it no longer needs it.”

Many answers keep the callback always enabled and call finish() or super.onBackPressed() in the else branch. That works on older phones. But the system can’t preview what back will do, so the predictive back-to-home animation never appears.

Toggling isEnabled avoids this. With history, the callback is on and back calls goBack(). On the first page it’s off, and the system closes the screen with its own animation. Every new page runs doUpdateVisitedHistory again and resets the state.

Fragments, tabs and drawers

In a fragment, register the callback with the fragment’s view lifecycle so it is removed when the fragment goes away. Use requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner) { ... }, which is how the codelab registers its WebView callback.

Apps with tabs or a drawer often hold one WebView per section. Each has its own history, so each needs its own callback, enabled only while that section is visible. Skip that and back on the Contact tab can rewind pages in a hidden Shop tab.

How to check the WebView back button worked

Test on a real Android 16 device or emulator with your target set to API level 36. Then:

  1. Open the app and tap through three links.
  2. Press back three times. Each press should load the previous page.
  3. Press back once more. The app should close, and with gesture navigation you should see the back-to-home preview while swiping.

If step 2 closes the app, the callback never got enabled. Check that you set webViewClient before the first loadUrl, and that links load inside the WebView. My guide to making external links open in the browser covers that split.

What goes wrong, and a temporary opt-out

Most WebView back button bugs come from two causes. Each has a short fix:

  • Back does nothing at all. You called goBack() with no history. The guide warns that after the user reaches the end of the history, goBack and goForward do nothing. Check canGoBack() first.
  • Back works on Android 13, fails on 16. You still rely on onBackPressed() or onKeyDown().

Android 16 offers a stopgap: set android:enableOnBackInvokedCallback to false in the manifest. Android calls this a temporary opt-out and asks you to update to the supported APIs. OnBackPressedCallback “is always called regardless of the value of android:enableOnBackInvokedCallback,” so the code above works either way.

My take, and the limit of this fix

My take: always toggle isEnabled from doUpdateVisitedHistory rather than checking canGoBack() inside an always-on callback. It costs two more lines, and it’s the only version that keeps predictive back working on the first page.

The limit is that goBack() only knows the WebView’s own history. A single-page site that changes screens without adding history entries gives the WebView nothing to step back through. That fix belongs in the website’s routing, not the app. If you’re weighing how much native code a wrapped site needs, native app vs WebView and the ways to turn a website into an app cover that choice.

FAQ

Why does the back button close my WebView app instead of going back?

Because nothing tells the system to step back through the WebView’s history. Register an OnBackPressedCallback that calls goBack() while canGoBack() returns true, and the back button walks through visited pages first.

Does overriding onBackPressed still work?

Not for apps that target Android 16. For apps targeting API level 36 on Android 16 devices, onBackPressed is not called and KeyEvent.KEYCODE_BACK is not dispatched anymore. Use OnBackPressedCallback instead.

How do I exit the app when there are no more pages to go back to?

Disable the callback when canGoBack() is false. The system then handles the back gesture itself and closes the screen, with the predictive back animation.

Can I turn off predictive back to keep my old code?

Only for now. Setting android:enableOnBackInvokedCallback to false is a temporary opt-out, and Android recommends updating to the supported back navigation APIs instead.

Sources