The app places WebView on screen with some preloaded content. There is data download running in background. When this data is ready, the WebView is being setup. The problem occurs if the user switches to other activity while data download is in progress:
void onDataLoaded() {
WebView wv = getWebView();
if (wv != null) {
wv.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.Black));
And the exception is:
java.lang.NullPointerException
at android.webkit.WebViewClassic.setBackgroundColor(WebViewClassic.java:8852)
at android.webkit.WebView.setBackgroundColor(WebView.java:2210)
at com.sample.app.TestActivity.onDataLoaded(TestActivity:45)
How can I prevent this crash if the WebView has been removed from the screen?