NickF
NickF

Reputation: 5737

Get fragment window token

I've an FragmentActivity with some fragments that contains EditText. If I open the keyboard and use my custom navigation (switching the fragments) the keyboard doesn't closes.

I call that method in activity onCreate:

private void initKeyboardHandler(){
        getSupportFragmentManager().addOnBackStackChangedListener(new OnBackStackChangedListener() {

            @Override
            public void onBackStackChanged() {
                InputMethodManager imm = (InputMethodManager)getSystemService(
                          Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
            }
        });
    }

After debugging I think the problem is in window token. Or it something else?

Upvotes: 0

Views: 3301

Answers (1)

Emil Adz
Emil Adz

Reputation: 41099

Try this to close the keyboard, get an instance of it:

  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);

Then close it:

imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

EDIT:

what if you check before this line if imm is not null:

if(imm != null)
{
    imm.toggleSoftInput(0, InputMethodManager.SHOW_IMPLICIT);
}

Upvotes: 3

Related Questions