onPageFinished() never called (webview)!
The second call to setWebViewClient()
is overwriting the first.
Create only a single instance of WebViewClient
with both overrides in the same class, and call setWebViewClient
only once. Then load the Webview:
mWebView.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(mWebView, url);
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), "Oh no! " + description, Toast.LENGTH_SHORT).show();
}
});
mWebView.loadUrl("http://pabebbe.com/m/register");
Android : WebView onPageFinished not calling after shouldOverrideUrlLoading method in android 4.4
I made a mistake in handling progress dialog.
public class MyWebViewClient extends WebViewClient {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
if(dialog == null){
dialog = ProgressDialog.show(getActivity(), null, "Loading...");
dialog.setCancelable(true);
}
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
if(dialog.isShowing())
dialog.dismiss();
}
}
Now its working fine for all api levels.
onPageFinished not getting triggered
Thanks to @323go, heres the answer (tho i just wish my quest wasnt downvoted :) )
onProgressChanged
and onPageFinished
need to be part of WebChromeClient
. Modified code:
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new TwitterCore(authConfig), new Digits());
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress){//not working
if(progress==100) {
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
String userPhone = settings.getString("userPhone", "null");
Long userId = settings.getLong("userId", 0);
String retVal = userPhone + "$" + userId;
mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
}
}
public void onPageFinished(WebView view, String url){//not workin
SharedPreferences settings = getApplicationContext().getSharedPreferences(PREFS_NAME, 0);
String userPhone = settings.getString("userPhone", "null");
Long userId = settings.getLong("userId", 0);
String retVal = userPhone + "$" + userId;
mWebView.loadUrl("javascript:displayUser('" + retVal + "')");
}
});
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.addJavascriptInterface(this, "Android");
mWebView.loadUrl("file:///android_asset/index.html");
}
onPageFinished sometimes not called Android WebView
I solved this by using webView.loadDataWithBaseURL(null,html, "text/html", "UTF-8",null);
instead of webView.loadData(html, "text/html", "UTF-8");
.
From what I read the two methods have a completly different implementation
Related Topics
How to Handle Empty Response Body with Retrofit 2
How to Add Network_Security_Config.Xml to Manifest in Expo App Without Ejecting
Widget Not Updated on Launcher Restart
How to Zoom + Crop a Image and Display the Croped Image on Imageview
Bordercolor Not Picking The Lineargradient Color on Android
Read Logs from All Apps on Android from Within an App for Android 4.2+
Why Is 0Dp Considered a Performance Enhancement
How to Set Camera Image Orientation
Splash Screen While Loading a Url in a Webview in Android App
How to Make My Layout Able to Scroll Down
Stop All Toast Messages When Going to Another Screen in Android
Android Stream Video from Google Drive
How to Handle App Lifecycle with Flutter (On Android and iOS)
Changed Jquery Mobile's Default Icon Set, Work on Browser, Failed on Device