2

I have a webview and on click on some banner I am sending Intent.ACTION_VIEW to open browser or whatever user finds suitable like this:

  w.setWebViewClient(new WebViewClient(){
         @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
             if (url != null && url.startsWith("http://")) {
                    view.getContext().startActivity(
                        new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                } else {

                    return false;
                }


            }

     });

Now what I want is to find a way to know if user just hit back button before opening browser or he actually opened browser. I found this question but no solution there

Community
  • 1
  • 1
v0d1ch
  • 2,738
  • 1
  • 22
  • 27

2 Answers2

2

You can do one thing which may not be the best practice to use but I think this will solve your problem

Initialization => boolean isPageLoading = false;

webview.setWebViewClient(new WebViewClient() {  
 public boolean shouldOverrideUrlLoading(WebView view, String url)  {                  

            return true;    
        }

        public void onPageFinished(WebView view, String url) {
            isPageLoading = false;             
        }
       public void onPageStarted(WebView view, String url, Bitmap favicon){
            isPageLoading = true;
        }
    });  

Now you have right indicator which will state, whether page is loading or not.

Now in your activity override back key press event

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) { // Back key pressed
                    if(isPageLoading){
                       // Do the tasks you want to do when page is loading
                    }else{
                       // Do the tasks you want to do when page loading is finished (or not in progress)
                     }
        return true;
    }
    return super.onKeyDown(keyCode, event);
  }

Note that you can change the behavior of this functionality by adding more boolean variables which will indicate different status info (such as whether page loading has started or not etc)

Hope this will solve issue you have raised

silwar
  • 6,470
  • 3
  • 46
  • 66
  • Jeah this is one workaround, if nobody comes with better answer I will accept it, I was aware I could do it that way I only thought there might be something else... – v0d1ch Dec 10 '12 at 20:56
1

Another possible solution: use startActivityForResult(intent, 1) instead of startActivity(intent) and check the requestCode and resultCode this way:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        //intent 1 was
        if(resultCode == Activity.RESULT_OK) {
             //successful
        } else {
             //not succusseful
        }
    }
}

This works for most sorts of Intent (email, camera, etc)

T_D
  • 3,241
  • 3
  • 17
  • 24
  • This solution was obsoleted with the new Activity Results API, see https://developer.android.com/training/basics/intents/result – rwst Mar 28 '22 at 09:22