Fragment replacement triggers onQueryTextChange on searchview Fragment replacement triggers onQueryTextChange on searchview android android

Fragment replacement triggers onQueryTextChange on searchview


After 2 days of Googling i got the solution that will definitely helps you.

I have just cut and pasted code from onCreateOptionMenu() to onPrepareOptionMenu()

I do not know why it happens, I think listener of searchView is not going to be NULL by the way you can change your code this way.

@Overridepublic void onPrepareOptionsMenu(Menu menu) {    super.onPrepareOptionsMenu(menu);    getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menu_all_order, menu);    searchView = (SearchView) menu.findItem(R.id.menu_all_order_search).getActionView();    searchView.setInputType(InputType.TYPE_CLASS_NUMBER);    searchView.setQueryHint("Enter Order No");    searchView.setOnQueryTextListener(this);}

and Remove:

@Overridepublic void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {    super.onCreateOptionsMenu(menu, inflater);}

Thanks :)


The problem is ActionBar (and subcomponents) are collapsed when the fragment holding it is replaced. When this happens the SearchView query is cleared as seen in onActionViewCollapsed. Workarounds include:

Prevent collapsing

Remove setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW); from the searchview's creation code or android:showAsAction="collapseActionView" from its xml code.

This will affect how/when the view's close/voicesearch buttons are displayed upon attempting to collapse it

It would also be possible to prevent collapsing by overriding SearchView's onActionViewCollapsed() method to do nothing but would accomplish the same.

Ignore query changes when appropriate

When the fragment is not valid (being replaced) then ignore query changes that would otherwise cause the fragment to alter its saved query text or layout (changing the layout would cause an exception if the fragment doesn't actually have view content).

public boolean onQueryTextChange(String newText) {    if (!isVisible()) {        // The fragment was replaced so ignore        return true;    }    // Text was actually changed, perform search    return true;}

This would be the better option as it doesn't affect the functionality of the searchview.


I see you didn't find a solution yet, I have same problem posted here:

I found that when invalidateOptionsMenu is called also the method onQueryTextChange is called because the View launch dispatchRestoreInstanceState with the previous value after you've cleared the search for example. Do you call invalidateOptionsMenu maybe?

Maybe, inside onQueryTextChange, you can check a boolean on some current state of your application objects to know if the method content should be executed. In my case I use mDrawerLayout.isDrawerOpen(..) to allow the search.

Also you can implement the SearchView.OnQueryTextListener on each Class you use, on a Fragment or on the Main Activity class, somewhere in the class you could set a boolean module variable that you will check inside onQueryTextChange.