onPageStart called many times and onPageFinished not called for single page - android

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!

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();
}
}

Related

Android webview app loader not showing first time

I have made a simple android webview app. Everything is working fine only my progressbar (loader) not showing when app opened. Rest of the pages when user navigate to another link then progressbar showing.
MainActivity.java file
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
...
}
#Override
public void onPageStarted(WebView view, String url) {
super.onPageStarted(view, url);
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
}
});
My problem is onPageStarted method. When I remove this then every page navigation display progressbar (except first time when app opened) but once I place onPageStarted method as above then android studio display an error like
method does not override method from its superclass
I am new in android and apology for this question if it is violet any policy. Need help.
Your onPageStarted() override method do not have all the required parameters of parents class. It should be as shown below
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

How can get notified when a WebView starts loading a new page?

I know how to get notified when it finishes loading a web page, but is there any way to know when it starts loading a new one that is initiated from a link from the original web page?
The reason that i want it is to make a ProgressBar visible whenever a page starts loading, and make it invisible whenever it finishes.
UPDATE: What I'm asking is if it's possible to know when a new page starts loading a page. Although from the link I found onPageStarted, and it works.
Check with the following
webView.setWebViewClient(new WebViewClient() {
//If you will not use this method url links are opeen in new brower not in webview
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
public void onLoadResource (WebView view, String url) {
//Show loader on url load
}
public void onPageFinished(WebView view, String url) {
//cancel loader complted
}
});
// Set Javascript enabled on webview
webView.getSettings().setJavaScriptEnabled(true);
To listen for a web page starting loading you should override onPageStarted, to listen for finishing you should override onPageFinished
and just to make it more complete, listen for error with onReceivedError
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view, url, favicon);
// runs when a page starts loading
progressBar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
// page finishes loading
progressBar.setVisibility(View.GONE);
}
#Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
super.onReceivedError(view, request, error);
// runs when there's a failure in loading page
progressBar.setVisibility(View.GONE);
Toast.makeText(context, "Failure on loading web page", Toast.LENGTH_SHORT).show();
}
});

Android WebView is blank after network disconnect

I have a following scenario for webview
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view_tutorial);
mWebView = (WebView) findViewById(R.id.webView1);
mWebView.setWebViewClient(mWebViewClient);
mWebView.setInitialScale(0);
mWebView.setVerticalScrollBarEnabled(false);
mWebView.requestFocusFromTouch();
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
Button btnGo = (Button) findViewById(R.id.button1);
btnGo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String url = "http://www.google.com";// here goes my server url with https://...... for authentication
showProgressDialog("Loading");
mWebView.loadUrl(url);
}
});
}
private void showProgressDialog(String title) {
if(mProgress == null || !mProgress.isShowing()){
mProgress = ProgressDialog.show(this, title, "Please wait...", true,
true, new OnCancelListener() {
public void onCancel(DialogInterface pd) {
finishActivity();
}
});
mProgress.setCanceledOnTouchOutside(false);
mProgress.setCancelable(true);
}
}
private void finishActivity() {
if(mWebView!=null){
finish();
}
}
private final WebViewClient mWebViewClient = new WebViewClient() {
public void onPageStarted(WebView view, String url,
android.graphics.Bitmap favicon) {
super.onPageStarted(view, url, favicon);
Log.d("test", "page started");
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("test", "page should override called");
return super.shouldOverrideUrlLoading(view, url);
}
#Override
public void onPageFinished(WebView view, String url) {
if (mIsLoadingSigninPage) {
mIsLoadingSigninPage = false;
dismissProgressDialog();
}
Log.d("test", "page finished");
super.onPageFinished(view, url);
}
#Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
mIsLoadingSigninPage = false;
dismissProgressDialog();
Log.d("test", "page error received");
super.onReceivedError(view, errorCode, description, failingUrl);
finishActivity();
}
};
private void dismissProgressDialog() {
if (mProgress != null && mProgress.isShowing()) {
mProgress.dismiss();
mProgress =null;
}
}
// this is log cat for point 3 below
03-07 07:31:20.977: D/test(1431): page started
03-07 07:31:22.788: D/test(1431): page should override called
03-07 07:31:22.887: D/test(1431): page started
03-07 07:31:24.496: D/test(1431): page finished
Ok,
When network is not available I ran this code, webviewclient's onReceivedError get called,
If network available and page loading started, and network disconnect, then also onReceivedError get called.
But there is a some instance of time,(count 1,2,3 after pressing button and disconnect network with f8 on emulator) mWebView.loadUrl(url) gets called, webViewclient's methods gets called like below
and WebView displays blank white screen, onReceivedError not called.
here is logcat
and
What is wrong with this code.
Why onReceivedError not called. If it does not get called, how to handle this situation
How to know webView has not loaded anyting and finish activity
it will certainly call page started and page finished because onReceivedError will be called on:-
-click of any link (if internet not present) then should override url will throw an error which it will handle
-on activity started or when webview is called
for all the exceptions which are not handled by onRecievedError you have to check http status

progress dialog could not stop

I am new to android.I am using progress dialog in my app but it could not stop.In my app i am using webview.In first activity I have connect button.In second activity connect to facebook code is available.In second activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.facebook);
wvFacebook.setWebViewClient(new MyWebViewClient());
wvFacebook.loadUrl(strFacebook);
}
private class MyWebViewClient extends WebViewClient
{
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
prDialog = ProgressDialog.show(getParent(), "In progress", "Loading, please wait...");
}
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
prDialog.dismiss();
Log.e("status","calling next......");
}
}
when connect button clicked the second activity will be executed and getting facebook page.But progress dialog could not stop.
Try like this
First create dialog in your onCreate()
ProgressDialog progressDialog =new ProgressDialog(getApplicationContext());
progressDialog.setMessage("Please Wait");
Then show the dialog in onPageStarted() method.
progressDialog .show();
Then dismiss it in your onPageFinished() method
progressDialog.cancel();
You should not create Dialog in onPageStart, when loading a web page, onPageStart() could be called multi times.
In you onPageStart, you create a new Dialog without closing the old one, so the old dialog will always show.
To solve this you have many choices:
As lmran said, create the dialog only once in onCreate(). Show it in onPageStarted and dismiss in onPageFinished.
Before create a new Dialog, dismiss the old one.
just this code used and try it out,
#Override
public void onPageFinished(WebView view, String url) {
Log.e("status","calling....");
super.onPageFinished(view, url);
if (prDialog != null || prDialog.isShowing())
prDialog.dismiss();
Log.e("status","calling next......");
}
#Override
public void onPageFinished(WebView view, String url) {
try{
if (prDialog.isShowing()|| prDialog!= null) {
prDialog.dismiss();
prDialog= null; /*** Add ***/
}
}catch(Exception exception){
exception.printStackTrace();
}
}
}
You may also visit the following link to get a complete detail...
http://androiddubd.blogspot.com/2014/07/how-to-show-progress-dialog-or-loader.html

android webview progress: any good solution?

I want to have a busy indicator of some sort when pages are loading. My indicator will be full-screen like a splash screen. This is because the android webview blanks the page to white before it starts loading, unlike a normal browser.
A simple solution might be suggested. for example, set loading true in onPageStarted() and set false in onPageFinished().
#Override
public void onPageFinished(WebView view, String url) {
setLoading(false);
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setLoading(true);
}
the problem with this is that the website i'm loading results in multiple start / finished cycles for a single click. maybe this is redirects, or iframes. i'm not sure ... but the result is that the "busy" indicator flashes on and off disturbingly.
Considering that, another ideas is to keep a count of pages loading- incrementing in onPageStarted() and decrementing in onPageFinished(). something like this,
private int loadingCount = 0;
#Override
public void onPageFinished(WebView view, String url) {
loadingCount--;
if (loadingCount == 0) {
setLoading(false);
}
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
if (loadingCount == 0) {
setLoading(true);
}
loadingCount++;
}
I'm running into this problem: page started / page finished are not matched. I'm not sure why exactly, but I think it might have to do w/ redirects.
Any ideas?
Try this:
private boolean loadingFinished = true, redirect = false;
public class myWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {
if (!loadingFinished) {
redirect = true;
}
loadingFinished = false;
view.setVisibility(View.VISIBLE);
view.loadUrl(urlNewString);
return true;
}
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
setTitle(url);
loadingFinished = false;
progressDialog = new ProgressDialog(FacebookWebView.this);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(" Loading...");
progressDialog.setCancelable(true);
progressDialog.show();
}
#Override
public void onPageFinished(WebView view, String url) {
if (!redirect) {
loadingFinished = true;
}
if (loadingFinished && !redirect) {
if (progressDialog.isShowing()){
progressDialog.dismiss();
}
//finish();
} else {
redirect = false;
}
}
I'm not exactly sure, but this should solve your problem with redirects. In the case of several frames on the same page, probably counting in onPageFinished till the number of frames, and then dismiss "busy dialog", should be a solution.

Categories

Resources