Challenge: Using the Back Button for Browser History

You may have noticed that you can follow other links within the WebView once you launch PhotoPageActivity. However, no matter how many links you follow, the Back button always brings you immediately back to PhotoGalleryActivity. What if you instead want the Back button to bring users their browsing history within the WebView?

Implement this behavior by overriding the Back button method Activity.onBackPressed(). Within that method you can use a combination of WebView’s browsing history methods (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 button to behave ...

Get Android Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.