Search code examples
androidandroid-keypad

after pressing home keyboard is not hiding


I use following code to show keyboard

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
        InputMethodManager.HIDE_IMPLICIT_ONLY);

I use following code to hide keyboard getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

to hide keyboard, and

also tried for this

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

but after pressing the home key. apps closes but keyboard remain same on the screen.

please guide me. what am I doing wrong?

I have put my hiding code. I put hide in onDestroy(), onBackPressed() and also onOptionsItemSelected(MenuItem item) Sorry For my code formatting.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mylayout);

    textTv=(EditText)findViewById(R.id.textview1);
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
    InputMethodManager.HIDE_IMPLICIT_ONLY);

    if(getIntent().getExtras()!=null)
    {
        Bundle extra=getIntent().getExtras();
        if(extra!=null)
        {
            // code
        }
    }
}

public void onBackPressed() {
    // TODO Auto-generated method stub
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
    }

public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    switch (item.getItemId()) {
    case R.id.menu1:
    //code
    break;
    case R.id.menu2:
    //code
    break;

}


Solution

  • Try

    @Override
    protected void onPause() {
        final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(noteTv.getWindowToken(), 0);
        super.onPause();
    }