Search code examples
androidandroid-search

Is there an advantage to using an Intent over an event listener launch in a search request?


I've looked over SO and cannot find a simple answer. Is there an advantage to using an Intent over an event listener on the search box for sending the text to the query event?

SearchView searchView =
            (SearchView) MenuItemCompat.getActionView(searchMenuItem);

    if(searchView != null){
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
            @Override
            public boolean onQueryTextSubmit(String s) {
                Toast.makeText(getApplicationContext(),
                        "String entered is " + s, Toast.LENGTH_SHORT).show();
                return true;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                return false;
            }
        });
    }

Versus using:

 private void handleIntent(Intent intent){
    if(Intent.ACTION_SEARCH.equals(intent.getAction())){
        String query = intent.getStringExtra(SearchManager.QUERY);
        Log.d(LOG_TAG, "QUERY: " + query);

        new FetchArtistTask().execute(query);

    }
}

Solution

  • Advantages of Intent:

    • The Intent can be directed to other Activities besides one with SearchView;
    • You can have single Activity handle search requests from multiple other Activities;
    • You can make full use of different Activity start modes and task stack while handling the Intent;
    • In addition to using SearchView the search Intent can triggered outside of Activities (e.g. in Dialogs and PopupWindows) by using The Search Dialog, making searching experience in your application more unified;
    • You can make a search Intent yourself to send from Service, when user clicks Notification/appwidget etc.

    Advantages of listener:

    • Can be used to filter suggestion list as user types.

    These two approaches aren't mutually exclusive, so you may just use both.