4

I have included a web application within android web view , and there is a link in the webpage which opens some other site , when the link is clicked it works fine for the first click, however when clicked for the second time the website is not found ,

the code is :

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

    if (url.contains("some site ")) {
     Intent i = new
         Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(i);
         return true;
    } else {
    view.loadUrl(url);
    return false;
   }
}

@THelper and @mikegr, thanks for the reply,

Actually in my case i have a modal panel (JSF) in my web application which contains some buttons, on clicking the button i am opening some other site using javascript window.open() method which works fine in desktop browser, however, when i wrap this web application within android webview, everything works fine except when i first click this button i'm able to open the other site using the external browser, however on second click the webview tries to open this othersite within the webview instead of the external browser and i get website not found with the entire URL of the other site, this happens even when i logout and login again as the application launched is still running.

also in my case after sometime when the application is idle i get the black screen.

i surfed through the net and found simillar issue but that didn't help either , here is the link:

http://groups.google.com/group/android-for-beginners/browse_thread/thread/42431dd1ca4a9d98

handling links in a webview ,

any help and ideas would be very helpful for me, this is taking too long for me,

since i'm trying to display my web application in the web view, i have only one activity, which contains code like this

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (savedInstanceState != null) {
        // so that when launcher is clicked while the application is 
           // running , the application doesn't start from the begnining
} else {

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    // Show the ProgressDialog on this thread
        this.progressDialog = ProgressDialog.show(this, "Pleas Wait..", "Loading", true);

    browser = (WebView) findViewById(R.id.webview);
    browser.getSettings().setJavaScriptEnabled(true);

    browser.setWebViewClient(new WebViewClient() {

        public void onPageFinished(WebView view, String url) {
                Log.i(TAG, "Finished loading URL: " +url);

                if (progressDialog.isShowing()) {
                    progressDialog .dismiss();
                }
            }


@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {

if (url.contains("some site")) {
     Intent i = new
     Intent(Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(i);
             return true;
} else {
        view.loadUrl(url);
    return true;
}
 }
});
browser.loadUrl("mysite");
}
}
Community
  • 1
  • 1
bjen
  • 41
  • 1
  • 3
  • Did you add logging to see if the link is still the same the 2nd time? Any error messages? Is it possible that you lost your internet connection temporarily (this happens occassionally when the emulator is running for a while). – THelper Sep 23 '11 at 09:23

3 Answers3

1

I had the experience that shouldOverrideUrlLoading() is not called in certain circumstances. There are a few bugs about this topic on http://code.google.com/p/android/issues like bug number 15827, 9122, 812, 2887

As a workaround try to add the method onPageStarted() and check if you get this call. For me this method is always called even if shouldOverrideUrlLoading() was not called before.

Michael Greifeneder
  • 2,900
  • 3
  • 18
  • 19
1

onPageStarted worked for me. Had to tweak it a bit, as that method is called when the webview is first rendered too, and I wanted to only execute it on the onClick of the banner's javascript.

I was simulating a banner with a custom page, so when the ad.html was being rendered, I avoided the startActivity. Otherwise, it launches the new browser window.

WebViewClient newWebClient = new WebViewClient() {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub
            super.onPageStarted(view, url, favicon);
            if(!url.equals("http://xxxx.ad.html"))
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
        }
    };
axel22
  • 32,045
  • 9
  • 125
  • 137
Steyr
  • 51
  • 3
0

Try to append the System parameter pattern in url before you load that, something like.

String url = "http://xxxx.ad.html?t="+System.nanoTime();

and in your shouldOverrideUrlLoading() method remove the query part (in a very first line).

int idx;
if ((idx = url.indexOf("?")) != -1) {
    url = url.substring(0, idx);
}
duckduckgo
  • 1,280
  • 1
  • 18
  • 32