1

I am using WebView to display url inside the application.When I click the back button in device, it terminates and close the application. For eg: i am giving link as

w.loadUrl("http://www.google.com");

In that i am searching and it goes to next page inside the webview.But when i click back

button key, it closes the application and not proceeding back to the history what I searched.

How to resolve this.? Any stuff to be given in this, If so, What should be given?

@Override
public void onBackPressed() { }

Thanks in Advance.

GrIsHu
  • 29,068
  • 10
  • 64
  • 102

4 Answers4

2

If you want to navigate back through your history in webview add this below code to your webview activity

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
         mWebView.goBack();
         return true;
    }
    return super.onKeyDown(keyCode, event);
}
Renjith
  • 5,783
  • 9
  • 31
  • 42
1

u can see both link or below code:-

How to go back to previous page if back button is pressed in WebView?

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode)
        {
        case KeyEvent.KEYCODE_BACK:
            if(mWebView.canGoBack() == true){
                mWebView.goBack();
            }else{
                finish();
            }
            return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

in that case you need to override back button through this code you can override back button

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {                                              
    if (keyCode == KeyEvent.KEYCODE_BACK)  {
         moveTaskToBack(true);
         return true;
    }
    return super.onKeyDown(keyCode, event);
}
Renjith
  • 5,783
  • 9
  • 31
  • 42
0

u can use below code, it's worked for me.

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    if (Integer.valueOf(android.os.Build.VERSION.SDK) < 7 
            && keyCode == KeyEvent.KEYCODE_BACK
            && event.getRepeatCount() == 0) {
        // Take care of calling this method on earlier versions of
        // the platform where it doesn't exist.
        onBackPressed();
    }
    return super.onKeyUp(keyCode, event);
}

@Override
public void onBackPressed() {
    return;
}
saravanan
  • 388
  • 2
  • 18