I have 2 tabs.
When 1 is opened, I want to show the keyboard.
When the other is opened, I want to make sure the keyboard is closed.
Here is my code for the fragment of the tab I want to show the keyboard (when this is opened, it auto-focuses an EditText the user will use to perform a search:
public class AddFoodSearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return V;
}
}
And the fragment to close it:
public class AddFoodFormFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_form, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return V;
}
}
Why is it not responding the way I want it to? I don't understand what is so difficult about this.
EDIT
I could just as well do this via adding onPause() to my 1 fragment, but its still not working:
public class AddFoodSearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return V;
}
@Override
public void onPause() {
super.onPause();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}