Android Actionbar Search widget implementation In ListFragment-ThrowExceptions

Exception or error:

I currently have my application set up with a ListFragment on the left and a DetailsFragment on the right (similar to the layout on the tablet below).


On the action bar I have a search widget which when a search term is submitted should update the contents of the list (in the ListFragment).
I have been following the search guide on android dev ( but I am having problems getting the search to update the list in the same activity.

I have managed to get some form of implementation working by calling a new activity when the search term is submitted but this makes the back button behave oddly (i.e. you press back and you go to the previous activity with the search widget still displaying the search term).

I have added the intent lines to the manifest as follows

        <action android:name="android.intent.action.SEARCH" />
    <meta-data android:name=""

Is it possible to get the search widget to update the list of the current activity?

How to solve:

Yes, as a matter of fact, that is the “normal” use case. What you have to remember is that the fragment that uses the search action bar widget, should be the part that registers it in general, since the widget will get destroyed along with that fragment.

Here’s how to properly tie it up:

in your ListFragment let it be known that you have option menu items…

public void onActivityCreated(Bundle savedInstanceState) {

then inside the same ListFragment create your options menu by overriding the callback, once you have the SearchView widget reference, register a QueryTextListener callback:

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(, menu); 
    SearchView searchView = (SearchView)menu.findItem(;

create your search widget via either programmatically (Java) or declaratively (XML) here is XML (named grid_default.xml, from above):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="">


    <!-- other items or whatever  -->


now back in your ListFragment you need to create the queryListener we registered above:

private String grid_currentQuery = null; // holds the current query...

final private OnQueryTextListener queryListener = new OnQueryTextListener() {       

    public boolean onQueryTextChange(String newText) {
        if (TextUtils.isEmpty(newText)) {
            grid_currentQuery = null;
        } else {
            getActivity().getActionBar().setSubtitle("List - Searching for: " + newText);
            grid_currentQuery = newText;

        getLoaderManager().restartLoader(0, null, MyListFragment.this); 
        return false;

    public boolean onQueryTextSubmit(String query) {            
        Toast.makeText(getActivity(), "Searching for: " + query + "...", Toast.LENGTH_SHORT).show();
        return false;

now you know when a query is changed or submitted, in my example, I re-query anytime a key is pressed because my database was small and fast with results, you might need to change this.. but you’ll notice how I am just using the widget to set a private class field inside the ListFragment to the current text of the search widget (grid_currentQuery) and then I am just calling getLoaderManager().restartLoader(0, null, MyListFragment.this); where inside my onCreateLoader I am just updating the query I used like this:

public Loader<Cursor> onCreateLoader(int id, Bundle bargs) {

    String sort = "SortColumn ASC";
    String[] grid_columns = new String[] { "ColumnA", "ColumnB", "Etc..." };
    String grid_whereClause = "ColumnToSearchBy LIKE ?"

    if (!TextUtils.isEmpty(grid_currentQuery)) {            
        return new CursorLoader(getActivity(), DataProvider.CONTENT_URI, grid_columns, grid_whereClause, new String[] { grid_currentQuery + "%" }, sort);

    return new CursorLoader(getActivity(), DataProvider.CONTENT_URI, grid_columns, null, null, sort);

and that’s all you really need to do, I realize this is kind of chopped up but it’s like this because there is more stuff that needs to happen, setting the adapter, starting the loader in the first place using getLoaderManager().initLoader(0, null, this); and all the rest of the loader callbacks that need to be handled… but if you are following the best practices set forward by the android folks, (using LoaderManagers in your ListActivitys and ListFragmentss it should be pretty easy for you…

I hope this helps feel free to ask for clarification in the comments if you need to -ck

Leave a Reply

Your email address will not be published. Required fields are marked *