简体   繁体   中英

I can't hide the virtual keyboard on Android

I am working with fragments and nesting fragments within fragments using the support library.

I have a scenario where I add a new fragment (which contains an EditText) from within the existing fragment. When the user taps on the EditText a virtual keyboard is shown. But while the keyboard is open the user can press the home button from the ActionBar which removes the fragment from the stack, but the keyboard still remains open. I can not force a close on the keyboard, I tried all code snippets. Given the described scenario, can anyone guide me as to how can I solve this ?

EDIT: I made a callback function which I call from the fragments onDestroy. The MainActivity which hosts all fragments implements this callback:

@Override
public void onHideSoftKeyboard(EditText editText) {
    // HIDE SOFT KEYBOARD HERE 

final InputMethodManager imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

     Toast.makeText(this,"KEYBOARD HIDDEN",Toast.LENGTH_LONG).show();
}

I get the Toast message and the fragment is destroyed on the back button (ActionBar back button), only the keyboard is still present.

@Override
public void onDestroy() {
    hideSoftKeyboard.onHideSoftKeyboard(editTextComment);



    super.onDestroy();
}

Try to force the keyboard with this:

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

You also can like this:

imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);  

If you want to hide when the user click on Up Home Button, try like this in your onOptionsItemSelected method:

case android.R.id.home:  
     // count the active fragment
     if(getSupportFragmentManager().getStackBackEntryCount() > 0) {
         // hide soft method as above
         InputMethodManager mImm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
         mImm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
         // do the pop backstack
         getSupportFragmentManager().popBackStack(); 
     } else {  
         // some stuff like finish the activity
     }
     return true;
// other items...

You can do the same with the back button when you use the (override) onBackPressed method.

You can use following code.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    final InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
}

I've fixed this issue with the following. First, if you want to pop the keyboard automatically when the activity launches, write the code below in the onCreate method.

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

Then, if you want to close the keyboard, use the following.

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

I solved this issue with the next solution

You need extend every your Fragment from BaseFragment as below:

public class BaseFragment extends Fragment {

    @Override
    public void onDestroyView() {
        hideKeyboard(getView());
        super.onDestroyView();
    }

    public void hideKeyboard(View view) {
        if(view != null) {
            InputMethodManager imm = (InputMethodManager) getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }
}

And all fragments that you want to be hide the keyboard when it will closed, must extend BaseFragment:

public class EditTextFragment extends BaseFragment {
...
}

As a bonus in every extended fragment you can use hideKeyboard(View view) method to hide keyboard when you want in any place in your fragment

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM