1

Just wondering if it's possible to prevent the keyboard from closing when the back button is pressed.

AKA, jump to the previous activity on one tap of the back button.

Evan Bashir
  • 5,501
  • 2
  • 25
  • 29
  • Why would you want your app to be the only one on the user's device to behave this way? – Krylez Oct 03 '13 at 19:01
  • Its a simple login screen that doesn't require the keyboard to ever be closed. – Evan Bashir Oct 03 '13 at 19:04
  • 1
    The user is familiar with the back button closing the IME. That's how the back button works _everywhere_ else across thousands of apps. These are the types of expectations that you should not fight. – Krylez Oct 03 '13 at 19:12

1 Answers1

0

You can override onBackPressed() so that if the keyboard is showing you just call finish() on your Activity:

@Override
public void onBackPressed()
    {
    boolean keyboardIsShowing = // determine if keyboard is showing somehow.
    if (keyboardIsShowing )
        {
        finish();
        }
    else
        {
        super.onBackPressed();
        }
    }

I am not sure an exact way to know if a keyboard is showing, but this link can point you in the right way:

How to check visibility of software keyboard in Android?

On a side note, users probably don't expect the Activity to close when the back button is pressed, they probably expect the keyboard to close. I would carefully consider your use case before implementing something like this.

Community
  • 1
  • 1
Jon
  • 1,820
  • 2
  • 19
  • 43