Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

For the More Curious: WebView Updates


For the More Curious: WebView Updates


WebView underwent a serious overhaul with the release of KitKat (Android 4.4, API 19). The new
WebView is based on the Chromium open source project. It now shares the same rendering engine used
by the Chrome for Android app, meaning pages should look and behave more consistently across the
two. (However, WebView does not have all the features Chrome for Android does. You can see a table
comparing the two at developer.chrome.com/multidevice/webview/overview.)


The move to Chromium meant some really exciting improvements for WebView, such as support
for new web standards like HTML5 and CSS3, an updated JavaScript engine, and improved
performance. From a development perspective, one of the most exciting new features is the added
support for remote debugging of WebView using Chrome DevTools (which can be enabled by calling
WebView.setWebContentsDebuggingEnabled()).


As of Lollipop (Android 5.0), the Chromium layer of WebView is updated automatically from the
Google Play Store. Users no longer wait for new releases of Android to receive security updates (and
new features). This is big news.


More recently, as of Nougat (Android 7.0), the Chromium layer for WebView comes directly from the
Chrome APK file, lowering memory and resource usage. Take solace in knowing that Google works to
keep the WebView components up to date.


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 through 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 as normal by calling through to
super.onBackPressed().

Free download pdf