1

enter image description here

Hi guys. When I open Activity 2 from Fragment 1 with intent and press back on Activity 2, my keyboard would pop up automatically if setIconifiedByDefault is false when I create the searchView in actionBar in Fragment 1. I tried to remove searchView.setIconifiedByDefault(false);. Everything works perfect (nothing pop up when I press back in Activity 2) but I cannot get the exact layout I want. Any suggestions to avoid the keyboard pop up?

In Fragment 1, I have below code to create searchView in actionBar

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate the options menu from XML
    inflater.inflate(R.menu.homesearchbar, menu);
    super.onCreateOptionsMenu(menu,inflater);

    SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);
    searchView.setIconifiedByDefault(false);
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) { return false; }
        @Override
        public boolean onQueryTextChange(String newText) { return false; }
    });
}

Edit - homesearchbar

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
        android:id="@+id/action_search"
        android:title=""
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="always"/>

</menu>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Pak Ho Cheung
  • 1,382
  • 6
  • 22
  • 52

1 Answers1

1

If you want to keep your keyboard state hidden then you should enter this in your onResume() method: And this is happening because your textfield always takes the focus whenever you start or resume your activity/fragment. To stop that one way is the following:

@Override
public void onResume() {
    super.onResume();

InputMethodManager inputManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(yourTextField.getWindowToken(), 0);

}

Or if you can't get your searchfield in onResume then use this instead:

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

And another way is to declare "stateHidden" attribute in activity tag in your manifest.

You can also set setFocusable(false) for searchView and setFocusable(true) in your fragment 1. So that it won't take the focus. and all credits to you for this.

Umair
  • 6,366
  • 15
  • 42
  • 50
  • ```yourTextField``` textField is the searchView in actionBar. How can I get the variable from onResume function? – Pak Ho Cheung Jan 31 '18 at 09:03
  • @PakHoCheung you need to declare your searchView globally.. :) – Umair Jan 31 '18 at 09:04
  • @PakHoCheung if you still can't do it please see my updated answer. – Umair Jan 31 '18 at 09:07
  • Tried to declare searchView globally and hide the keyboard in onResume, keyboard also pop up. I also tried to put ```getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);``` on Fragment 1's onResume method or on Activity 1's onResume method. Keyboard still pop up. I can see searchView is editing. – Pak Ho Cheung Jan 31 '18 at 09:12
  • then add clear focus () to your searchview explicitly.like searchView.clearfocus(); – Umair Jan 31 '18 at 09:15
  • also tried android:windowSoftInputMode="stateHidden" – Pak Ho Cheung Jan 31 '18 at 09:16
  • then please take a look at this answer it may help you. https://stackoverflow.com/questions/21624282/searchview-gains-focus-and-opens-keyboard-when-returning-from-another-activity – Umair Jan 31 '18 at 09:20
  • also there is a work around too. take a edittext and set it's visibility to gone and give it the focus. then enter the above code. it will work i believe. – Umair Jan 31 '18 at 09:20
  • Ok let me try. Will get back to you. – Pak Ho Cheung Jan 31 '18 at 09:25
  • sure do let me know. :) – Umair Jan 31 '18 at 09:25
  • I am creating a menu. Am I doing on a wrong way? See my edit. Thanks for the help. – Pak Ho Cheung Jan 31 '18 at 09:37
  • God damn. Finally fix it by setFocusable(false) for searchView and setFocusable(true) for one of layout in Fragment 1. You can change your answer, so that I can vote. Thanks a lot – Pak Ho Cheung Jan 31 '18 at 10:10
  • @PakHoCheung it is wired though but happy that it solved your problem and I have edited the answer all credits to you :) – Umair Jan 31 '18 at 10:12