2

I find out that the code below will only work for hardware buttons

webView.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if(!v.hasFocus()){
                v.requestFocus();
            }
            return false;
        }
    });

onKey() method not getting called.

Madhav Bhattarai
  • 847
  • 2
  • 10
  • 29
  • your question is not clear please explain more to get help – ρяσѕρєя K Dec 19 '14 at 05:43
  • You might want to expand out your question a bit more. It doesn't look like you put much work into it. If you aren't willing to put work into explaining and clarifying your question, how can you expect others to put work into answer your question? – Alex K Dec 19 '14 at 05:44

1 Answers1

2

Try something like:

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

        }
        return super.onKeyDown(keyCode, event);
    }
Krupa Patel
  • 3,309
  • 3
  • 23
  • 28