0
  • I navigate from Activity1 to Activity2 On Activity 2 I have a keyboard and this keyboard stays on the screen after selecting the back button and going to Activity 1.

This is how I fixed this issue

    // This code is in Activity 2
@Override
public void onBackPressed() {
    startActivity(intentForActivity1);
    finish();
}

Is this a wrong solution to my problem? Is it a bad idea to handle the back button manually?

aryaxt
  • 69,636
  • 87
  • 281
  • 421

2 Answers2

1

There's nothing inherently wrong with overriding the back button. Just make sure the behavior isn't confusing to the user.

Also, if you ever just want to hide the soft keyboard (say, you're switching between tabs or something like that), you can use InputMethodManager. Here's a thread where people discussed ways to do this.

Community
  • 1
  • 1
Turnsole
  • 3,282
  • 4
  • 26
  • 50
1

Since you're capturing the back button press, most probably the soft keyboard does not receive the press and thus it does not hide.

Try hiding it yourself:

@Override
public void onBackPressed() {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    startActivity(intentForActivity1);
    finish();
}

See Reto Meier's answer for more details on this method to hide the keyboard: Close/hide the Android Soft Keyboard

Community
  • 1
  • 1
Aleadam
  • 39,361
  • 9
  • 84
  • 108