onPageStart called many times and onPageFinished not called for single page
My question is different from this one guys.. I wany my progress dialog start when page load starts and end when the page load finished in my webview. My problem is the progress dialog starts and never get dismissed.I have set break points it shows that the progress dialog starts and get dismissed many times then it starts and not get dismissed even after page load completed. My question is why the onPageStarted getting executed many time for a single page loading? and why onPageFinished not called after completion of page load?
myWebView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myWebView.loadUrl(url);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(myWebView, url, favicon);
Log.d("mytag","Page Loading Started");
//myURLProgressDialog= ProgressDialog.show(WebviewExampleActivity.this, "Page Loading", "Wait for a moment...");
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d("mytag","Page Loading Finished!");
super.onPageFinished(myWebView, url);
//myURLProgressDialog.dismiss();
}
});
My self tagged filtered Log is Like this for loading single page:
10-06 10:32:49.298: DEBUG/mytag(508): Page Loading Started
10-06 10:32:49.998: DEBUG/mytag(508): Page Loading Started
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Finished!
10-06 10:32:50.048: DEBUG/mytag(508): Page Loading Started
10-06 10:33:00.898: DEBUG/mytag(508): Page Loading Finished!
When I am clicking link on already loaded page it works fine. Here is Log:
10-06 10:59:25.098: DEBUG/mytag(543): Page Loading Started
10-06 10:59:30.889: DEBUG/mytag(543): Page Loading Finished!开发者_JS百科
Check whether it is already open with .isShowing()
.
final ProgressDialog mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading...");
browser.setWebViewClient(new myViewClient(){
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(!mProgressDialog.isShowing())
{
mProgressDialog.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
}
});
If you have ajax calls in the page being loaded, onPageFinished() will be called only when those calls finish. Better to make those calls with a window.setTimeout(). In that case, UI thread will not be blocked on those calls and onPageFinished() will be called as soon as the main page loads.
Here is a solution. Instead of a loading dialog, i use another webview as splash-screen, but you can change it easily.
The trick is, to look if there is a new "onpagestart" right after the "onpagefinished". If this is the case, don't close the loading and wait for the next "onpagefinished".
myWebView.setWebViewClient(new WebViewClient() {
boolean loadingFinished = true;
boolean redirect = false;
long last_page_start;
long now;
// Load the url
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.loadUrl(url);
return false;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
Log.i("p","pagestart");
loadingFinished = false;
last_page_start = System.nanoTime();
show_splash();
}
// When finish loading page
public void onPageFinished(WebView view, String url) {
Log.i("p","pagefinish");
if(!redirect){
loadingFinished = true;
}
//call remove_splash in 500 miSec
if(loadingFinished && !redirect){
now = System.nanoTime();
new android.os.Handler().postDelayed(
new Runnable() {
public void run() {
remove_splash();
}
},
500);
} else{
redirect = false;
}
}
private void show_splash() {
if(myWebView.getVisibility() == View.VISIBLE) {
myWebView.setVisibility(View.GONE);
myWebView_splash.setVisibility(View.VISIBLE);
}
}
//if a new "page start" was fired dont remove splash screen
private void remove_splash() {
if (last_page_start < now) {
myWebView.setVisibility(View.VISIBLE);
myWebView_splash.setVisibility(View.GONE);
}
}
});
Sometimes happens that when the initial page loads, some script makes a redirect (because it needed a session ID, or something that would make the page load again).
You can check if a different page (or the same one with additional parameters) is loading by logging the url parameter.
I guess that if a new load request is made before the first page finishes loading, then the OnPageFinished method won't be called for that first page.
When a page submit on webview app: onPageStarted Fired, but onPageFinished not Fired so follow code below fixed and exactly
ProgressDialog pd = null;
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(pd == null || !pd.isShowing()) {
pd = new ProgressDialog(MainActivity.this);
pd.setTitle("Please wait");
pd.setMessage("App is loading...");
pd.show();
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if (pd != null) {
pd.dismiss();
}
}
精彩评论