Search code examples
androidwebviewback

Android webview back button


In my app when user clicks on a gallery widget i open the image in a webview as it supports zoom functionality.

Now when user clicks back button on webview i want the user to see my application. But it goes directly to home screen.

How should i handle the onBackKeyPressed() ?


Solution

  • Override the Activity's onKeyDown(..) event

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // close image gallery
            return false; // this avoids passing to super
        }
    
        return super.onKeyDown(keyCode, event);
    }
    

    *edit: code