I have added a progress bar on a webview. My problem is when the webview is loading a url and the progress bar shows progress and I hit back button, progress bar still keeps loading till it gets completed and shows up on the back page where it should not.
ProgressBar progressBar = (ProgressBar) rootView.findViewById(R.id.progressBar);
webView.setWebChromeClient(new CustomWebChromeClient(progressBar));
private static class CustomWebChromeClient extends WebChromeClient {
private static final int MAX_PROGRESS = 100;
private final ProgressBar progressBar;
public CustomWebChromeClient(ProgressBar webProgress) {
progressBar = webProgress;
}
@Override
public void onProgressChanged(WebView web, int newProgress) {
boolean stillLoading = isStillLoading(newProgress);
if (hasWebProgress()) {
progressBar.setProgress(newProgress);
progressBar.setVisibility(stillLoading ? View.VISIBLE : View.GONE);
}
super.onProgressChanged(web, newProgress);
}
private boolean isStillLoading(int newProgress) {
return newProgress < MAX_PROGRESS;
}
private boolean hasWebProgress() {
return progressBar != null;
}
}
OnBackPressed, when I try to do
progressBar.setVisibility(View.GONE);
this does not work. I can see code hitting this line on a breakpoint. All this is happening in a fragment.