2

I have a problem which occurs only when I use AnySoftKeyboard. I'm trying to show/hide the keyboard according to EditText focus. I used the methods I found in this post

When I'm hiding the keyboard, there is a strange behavior -

  • When I rotate the screen, the text that was in the EditText is doubled.
  • I thought that it has to do with the onCreate method, but I can see the it happens also when I click "back" (Finish()). I see it for a split second before the Activity is closed.
  • When I start a new activity, (ActivityB from ActivityA) then clicking "Back" once doesn't do anything (probably "closing" the invisible keyboard).
  • When I click "back" again, ActivityB closes but I can see for a split second the text from ActivityA keyboard in a big font across the screen (like a 100 millisecond pop-up )

Does anyone has an idea how to deal with it?

Community
  • 1
  • 1
Asaf Pinhassi
  • 14,214
  • 11
  • 102
  • 121

2 Answers2

1

Apparently it is a bug in AnySoftKeyboard. I didn't happen when I use other keyboards.

I solved it by doing setText to the EditText view before hiding it - its probably resets some stuff on keyboard object.

Here is my code:

        View view = getWindow().getCurrentFocus();
        if (view==null)
            return;

        IBinder binder = view.getWindowToken();
        if (binder == null)
            return;

         // I used this to fix the strange behaviour
        if (view instanceof EditText)
            ((EditText)view).setText(((EditText)view).getText().toString());


        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(binder, InputMethodManager.HIDE_NOT_ALWAYS);

Surprisingly it works!

Asaf Pinhassi
  • 14,214
  • 11
  • 102
  • 121
0

Try this:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
A.Quiroga
  • 5,609
  • 5
  • 33
  • 56