Android’s WebView Common Challenges and It’s Solution
In Android development, it is very common things in app that we encounter a webpage is loaded inside our App and exchanging interaction between embedded webpage and native page in App. This is make possible by Google team using Android’s WebView
. According to Google in Android Developer’s Docs , The WebView
class is an extension of Android's View
class that allows you to display web pages as a part of your activity layout. So basically you can include WebView
layout in an XML layout and then load an URL in it to be shown to user.
Fairly to say that using WebView
it’s quite straightforward, but Android’s WebView
‘s might get even more complex than we actually thought earlier. Things might get more complex when:
- Showing shimmering loading or loading dialog when
WebView
start to load an URL until it finish loading the web page. - Redirect to native page if there’s any URL redirected from
WebView
that is supported in native App. - Error handling when
WebView’s
crashes.
Now, we will going to run through on solving common challenges above when we are working with WebView
in Android. To able to tackle those challenges, we might need to use a WebViewClient
provided from WebView
. WebViewClient
is basically a class that make any WebView’s
callback possible for us to override or listen WebView
behaviour. Those common challenges can be solved by the usage of a WebViewClient
. First thing we need to do is to create an WebViewClient
and set it into our WebView
.
Here’s the code snippet.
As we can see here, WebViewClient
give us a lot of options to handle callback from WebView
. As I shared above, some of these methods are going to solve some of the challenge we’re encountering. But before we start, I’m going to explain some methods in the code and its usage:
onPageStarted
method is callback method fired duringWebView
is starting to load the webpage. We can use this callback to start shimmering or any native loading behaviour to tell user to wait until webpage finish loading.onPageFinished
method acts as the opposite ofonPageStarted
usage. This method is fired whenWebView
finish loading. As previously we start our loading atonPageStarted
then we can dismiss the shimmering or other loading here.onPageStarted
andonPageFinished
methods are very useful for us to handle native loading behaviour so that user doesn’t need to see blank loading page while waiting the page to load.shouldOverrideUrlLoading
method is callback method fired fromWebView
whenWebView
is trying to load an URL. Sometimes it happens whenWebView
is redirecting into another web page. Here’s the great thing aboutshouldOverrideUrlLoading
, as you can see this method expect us to return aBoolean
. ABoolean
return type gave us an authority to allow or cancel loading another URL in ourWebView
. To allow the URL loading, we can just return true and otherwise. This method is mostly use to check if URL is supported to open in native page, if supported we can start a new activity and returntrue
inshouldOverrideUrlLoading
method. This method surely can be use to solve the requirement to open native page fromWebView
when it is loading an URL that is supported in native page.onReceivedHttpError
method help us to receive callback when there’s any API or resource error specifically with ≥ 400 HTTP status code. The use case we can use here is to show or handle any native error handling like showing toast to tell user that’s an error in the web page and expect them to refresh or any other error handling.onRenderProcessGone
method notify us thatWebView
’s render process has exited which meansWebView
should be disposed and cleaned up programmatically as we cannot load any URL in a crashedWebView
. We can either close theWebView’s
activity and tell user to re-open it or we removeWebView
class from view hierarchy and re-add it programmatically to continue usingWebView
. One thing that we should note thatWebView
engine can be shared in multiple activity thusonRenderProcessGone
can be called in multiple activity that host those sharedWebView
’s engine andonRenderProcessGone
method is only available on Android’s API 26 and above.onRenderProcessGone
method enables us to handleWebView
crashes inWebView
gracefully.
We have run through some methods we can use at WebViewClient
. Let’s take a look at common challenges and try to solve it using the knowledge we gained from WebViewClient
capabilities.
- Showing shimmering or loading dialog when
WebView
start to load an URL until it finish loading the web page.
To solve this challenge, we can use onPageStarted
and onPageFinished
method from WebViewClient
as a trigger to show loading and hide loading every time the page load. We can also use this callback for tracking purpose on how long it takes for WebView
to load the page completely.
2. Redirect to native page if there’s any URL redirected from WebView
that is supported in native App.
To detect and intercept any redirection from WebView
, we can use shouldOverrideUrlLoading
and return true if it is supported to redirect into native page so that WebView
stop the URL redirection in the web page and stay in the current page.
3. Error handling when WebView’s
crashes.
It is a common thing that WebView
engine may crash. When WebView
crash, it will propagate the crash to the app and stop the App from running which result in bad experience to user. To handle this crash gracefully, we can use onRenderProcessGone
to listen WebView’s
render crash event and recreate the WebView
.
That’s all about common challenges in Android’s WebView. Stay tuned for my next article about WebView
that will discuss about passing data and interaction that’s backward compatible between Web app and Android’s WebView
using JavascriptInterface
and evaluateJavascript
. Thank you.