1

I have looked all over this site, google, and another Android forum for the answer but I cannot find one that works.

I am trying to include the BACK key in my Android WebView app, because otherwise the BACK key just exits the activity.

I am new to Android so I don't know all there is to programing it. Can anybody point me in the right direction please?

Thanks!

SMOKE
  • 55
  • 1
  • 9
  • Already been answered: http://stackoverflow.com/questions/6077141/android-webview-how-to-code-the-back-button/6077173#6077173 – FoamyGuy May 07 '12 at 01:28

3 Answers3

4

Intercept the BACK button in your Activity as follows...

public class MyWebActivity extends Activity {

    WebView webview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Other code here
        webview = new WebView(this);
    }

    // Other methods here

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Explained in part 9 of the Hello WebView Tutorial

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • I have a problem with this one, but it looks like it should work. When I put this code in my activity, it gives me an error on the webview part of the code you provided. I tried using WebViewClient and MyWebViewClient to get it to work (they are both already in my code) and all it does is gives me another error on the .canGoBack() and .goBack() . Does anyone have any ideas? – SMOKE May 07 '12 at 12:32
  • @SMOKE: Did you look at the tutorial I linked to in my answer? In my code excerpt above `webview` is an instance of the `WebView` class not `WebViewClient`. See my edited code example in my answer. – Squonk May 07 '12 at 15:51
  • It is not giving me the error anymore, but after testing the app it does not work :( I added my .Java Activity for you to look at. Maybe I did something else wrong in my code? – SMOKE May 07 '12 at 19:12
  • Update: For some reason my activity code that I put on this site was taken down. :( – SMOKE May 07 '12 at 22:48
1

override onbackpressed in your activity.

@Override
public void onBackPressed() {

}

If you dont want to close your activity on back press then you can simply remove super.onBackPressed() this will not allow to finish your activity and your activity will work properly on backpress without closing.

Bharat Sharma
  • 3,926
  • 2
  • 17
  • 29
0

For example im doing the next catching a URL in my WebView over Android;

In the your Method shouldOverrideUrlLoading:

public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                webView.getUrl();
                if( url.equals("http://any.com/url") ){
                   //Do any action
                    Toast tr =Toast.makeText(WebViewTest.this, "Test Passed", Toast.LENGTH_LONG);
                    tr.show();
                    //Go to other activity or you can put finish(); to stop the actual activity
                   //Do other action
                    Intent i1 = new Intent("com.mypack.courses.Passed");
                    startActivity(i1);;
                    return true;
                }
                if( url.equals("http://any.com/url") ){
                    Toast tr =Toast.makeText(WebViewTest.this, "Test Failed", Toast.LENGTH_LONG);
                    tr.show();

                   Intent i2 = new Intent("com.mypack.courses.Failed");
                   startActivity(i2);
                    return true;
                }
                return true;
            }

in this case, if the user click in the ""http://any.com/url" over the Webview, we do any actions, just put your conditions or methods, and wherever you want to do there, hope this give you ideas...

JLouis
  • 284
  • 1
  • 5
  • 18