Reputation: 1900
Does PhoneGap (more specifically DroidGap for Android) have way to tell when its webview has been loaded. I'd like to find something like the following for PhoneGap:
Normal WebView Client Creation:
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
}
});
Creating my PhoneGap Webview like this:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
if (isOnline() == true) {
WebSettings settings = mWebView.getSettings();
settings.setAllowFileAccess(true);
settings.setAppCacheEnabled(true);
settings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://myurl.com");
mWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
}
});
}
else {
//hide loading image
findViewById(R.id.imageLoading1).setVisibility(View.GONE);
//show webview
findViewById(R.id.mWebView).setVisibility(View.VISIBLE);
Intent myIntent = new Intent(getContext(), LoadScreen.class);
startActivity(myIntent);
finish();
}
}
My Main_Activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView android:id="@+id/imageLoading1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="visible"
android:src="@drawable/splash"
/>
<WebView android:id="@+id/mWebView"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:visibility="gone"
/>
</RelativeLayout>
Upvotes: 3
Views: 4556
Reputation: 596
DroidGap does not have a method, since it's an activity, and not a view. Instead, the CordovaWebViewClient uses its onPageFinished method to send a message to all the plugins when the page has finished loading. This is what plugins can use to determine whether the page has finished. Conversely, you could check for the deviceready event in Javascript, which should be fired after the page has been loaded.
If you need to know when the WebView has loaded in Java for anything other than plugins, I highly recommend switching to using CordovaWebView and implementing the lifecycle events yourself, since this will give you more control over the augmented view. This also means that you have to make sure that plugins which use activities are handled correctly, otherwise they will break.
Upvotes: 3