开发者

Android - Activities and navigation?

  • I navigate from Activity1 to Activity2 On Activity 2 I have a keyboard and 开发者_Go百科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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜