Android Programming: The Big Nerd Ranch Guide, 5th Edition
by Bryan Sills, Brian Gardner, Kristin Marsicano, Chris Stewart
Challenge: Using the Back Button for Browser History
You may have noticed that you can follow other links within the WebView once you launch PhotoPageFragment. However, no matter how many links you follow, the Back button always brings you immediately back to PhotoGalleryFragment. What if you instead want the Back button to bring users through their browsing history within the WebView?
Implement this behavior by adding a callback to the activity’s onBackPressedDispatcher property. Within that callback, use a combination of WebView’s browsing history functions (WebView.canGoBack() and WebView.goBack()) to do the right thing. If there are items in the WebView’s browsing history, go back to the previous item. Otherwise, allow the Back ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access