Sunday 15 April 2012

android - How to handle page reload in Cordova/PhoneGap -


I have a Cordova / PhoneGapp application that uses History API. This history uses APIs because it is a shared code between the Web and Cordova apps. And like mobile webkit it looks like a support history API (but there is actually a support carriage).

In some places the customer creates js-code document.location.reload and the current URL can be any (and is different from the initial one then one). In the Cordova app this leads to "Application error: Network error (file: /// display / ...)" That's because there was history.pushState ("/ display / ..") before.

In fact I could patched the customer code where it makes history.pushState, but I woudldn't want to do it.

Instead I want to handle url loading in the Cordova Java code and want to redirect it to load "index.html" (the main app page in property / www).

How to do this?

I tried to override Cordowa Vebfelt client with its own implementation:

  Cordowa Vebfekkliant Vebwuclaint = new Maiwwwvelclaint (This, this.appView ); This.appView.setWebViewClient (webViewClient);   

where overwrite shouldOverrideUrlLoading:

  Public Boolean shouldOverrideUrlLoading (WebView WebView, string URL) {if (url.startsWith ("file: /// display / ")) Really true; Return super.shouldOverrideUrl loading (webview, url); }   

But the method is not called on page reload from the client JS code.

Here's a solution I came up with:

  public Class MyApp DroidGap {@Override Public Zero onCreate (Bundle savedInstanceState) {CordovaWebViewClient webViewClient = New MyWebViewClient (this extends, .appView); This.appView.setWebViewClient (webViewClient); }} @TargetApi (Build.VERSION_CODES.HONEYCOMB) public class MyWebViewClient CordovaWebViewClient {private DroidGap reference extends public MyWebViewClient (DroidGap ctx, Cordowa Web View) {super (Sitiaks, visual); This.context = ctx; } @Override Public WebResourceResponse shouldInterceptRequest (for viewing WebView, string URL) {if (url.startsWith ("file: /// display /")) {InputStream section; Try {stream = this.context.getAssets () Open ("www / index.html"); } Hold (IOException e) {e.printStackTrace (); Throw new runtime up (E); } WebResourceResponse response = new WebResourceResponse ( "text / html", "utf-8", stream); // this array of assets is important because Page, view.loadUrl (Config.getStartUrl ()); Return feedback; } Return super. ShouldInterceptRequest (see, url); }}    

No comments:

Post a Comment