0

The situation:

I have Activity A. It hast one field. And when user presses "OK", it opens Activity B, which is just a confirmation dialog. Now when user presses "Cancel" button in activity B (setResult(RESULT_CANCEL); finish();), user ends up back on Activity A.

At this point I would like to show the keyboard again, because the only thing, which user can do is editing the field.

But for some strange reason it does not work for me. I looked at this discussion and tried settings

android:windowSoftInputMode="stateAlwaysVisible|adjustResize"

in manifest or calling

inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);

in either onCreate() or onStart() or onResume(), but none of these works.

I should add, that this problem only shows up, when I "go back from other activity". When the activity is created during forward navigation to it, then the keyboard is shown as expected.

Any suggestions on how to handle this problem would be appreciated.

Dmitrii Semikin
  • 1,550
  • 1
  • 15
  • 21

1 Answers1

1

kindly use this

@Override
    protected void onResume() {
        super.onResume();
        mUserNameEdit.requestFocus();

        mUserNameEdit.postDelayed(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                InputMethodManager keyboard = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(mUserNameEdit, 0);
            }
        },200); //use 300 to make it run when coming back from lock screen
    }
Muhammad Aslam
  • 312
  • 1
  • 4
  • This works for me. Use of `postDelayed()` seems to be the crucial part. Could you, please, explain (or provide some link to the explanation), why is it the case? Why e.g. simple `post()` does not do the job in this case, and on a first place, why direct call to `inputMethodManager.showSoftInput()` does not work? – Dmitrii Semikin Jan 23 '21 at 18:32