In my single-activity app, I have a ViewPager which has a PagerTabStrip. Let's say I have two tabs and two corresponding fragments. On fragment one (Frag1) I have an EditText view and on fragment two (Frag2), there is only a listview with no editable text (just a list of CheckedTextViews).
When the app starts, Frag1 is visible and focus is on the EditText and, thus, the soft keyboard appears. No problem yet. However, when I swipe to the other tab and Frag2 slides into view and Frag1 is no longer visible, the keyboard remains even though there is nothing on the visible screen (Frag2) to edit. Even if I check one of Frag2's CheckedTextViews, the keyboard remains.
Programmatically, how can I robustly and dynamically check for this type of situation and hide the keyboard?
You can hide the keyboard on the swipe event. Put this in your tabselected method or whatever you're using to switch fragments:
EditText myEditText = (EditText) findViewById(R.id.myEditText);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
Hope this helps! :D