0

I am trying to trigger an onCancel() when the user exit of the search dialog by pressing the left arrow button, but this never occurs. I tried register the cancel listener in the main activity OnCreate(), or in onViewCreated() ; also in the fragment using the search box. The code :

final SearchManager searchManager = (SearchManager)getSystemService(SEARCH_SERVICE);
searchManager.setOnCancelListener(
    new SearchManager.OnCancelListener() {
        @Override
        public void onCancel() {
            Log.e("cancel","--------------");
        }
    }
);

The Log.e line is never called. Thanks

Jean Bain
  • 1
  • 1

1 Answers1

0

In the SearchView guide says:

If the user cancels search by pressing the Back button, the search dialog closes and the activity regains input focus. You can register to be notified when the search dialog is closed with setOnDismissListener() and/or setOnCancelListener(). You should need to register only the OnDismissListener, because it is called every time the search dialog closes. The OnCancelListener only pertains to events in which the user explicitly exited the search dialog, so it is not called when a search is executed (in which case, the search dialog naturally disappears).

So if you want to handle dismissal that occurs after user has searched (search is executed) then you must handle it by implementing OnDismissListener.

If you want to handle cancellation that occurs when the searchview is closed without doing any search (search is not executed) then you handle it in an OnCancelListener.

You can handle the both as well.

Kozmotronik
  • 2,080
  • 3
  • 10
  • 25