Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android WebView prevent page reload

I have a Phonegap packaged application that opens a PDF Viewer when a button is pressed. I did it with the FileOpener2 plugin.

When I go back to the application from the PDF Viewer, the result differs depending on Android versions.

In Android 4.* the application stays where it should be before leaving the application. However, in Android 2.3.7 the WebView reloads the application, which is the behaviour I don´t expect.

I already tried changing android:launchmode to singleTask or singleInstance with no success.

How can I avoid page reload when I came back to the Phonegap application?

Thanks in advance!

Update 1

I have modified the Cordova or Phonegap class CordovaWebViewClient which extends WebViewClient with:

@Override
public void onLoadResource(WebView view, String url) {
    if (url.equals("file:///android_asset/www/index.html")) {
        if (this.firstLoad) {
            this.firstLoad = false;
        } else {
            view.stopLoading();
        }
    }

However the onLoadResource only gets fired when the application started and not when it resumes from another application.

like image 827
gonzalomelov Avatar asked Mar 12 '14 12:03

gonzalomelov


2 Answers

Answering my own question, setting the following properties to the only Acivity in AndroidManifest solves the problem. However I have to test it because it could have problems that I don't know:

android:launchMode="singleInstance"
android:alwaysRetainTaskState="true"

Cheers!

Update

After testing a bug appeared selection a photo in the Gallery with Phonegap Camera plugin that shows "Selection cancelled".

So after reading https://groups.google.com/forum/#!topic/phonegap/R08vOZNm580 I set:

 android:launchMode="singleTask"
 android:taskAffinity=""
 android:excludeFromRecents="true"
like image 94
gonzalomelov Avatar answered Sep 19 '22 22:09

gonzalomelov


In android.webkit.WebViewClient onLoadResource callback invoke stopLoading of WebView

MyWebViewClient extend WebViewClient {

public void onLoadResource (WebView view, String url) {
    if(your condition ){
         view.stopLoading();
    }
}
}
like image 28
WatorVapor Avatar answered Sep 21 '22 22:09

WatorVapor