How do I manage a stack of previous SearchView searches and access them via the Up button?

573 views Asked by At

I am working on a simple application that uses droidQuery to make network requests to fetch public stream data from a variety of websites. The requirements of the project include supporting API 8, and having an ActionBar. Instead of using ActionBarSherlock, I thought I would try my luck at the compatibility ActionBar in the android-support-v7-appcompat library. This has been working well for the most part, but I am having trouble handling the Up Button in order to navigate to previous searches (which search users on the public streams). Here is what I have:

Note this is a stripped down version, and some methods are missing (although I have implemented them in my code).

public class PublicStreamClient extends ActionBarActivity implements OnQueryTextListener, OnSuggestionListener, OnPullToRefreshListener {
    private Stack<SearchStackItem> previousSearches;//initialized in onCreate
    private class SearchStackItem {
        public String query;
        public SearchStackItem(String query) {
            this.query = query;
        }
    }

    //called when the user searches a network with the given string
    public void search(final String query, final boolean addToStack) {
        //do search using given query String
        //once completed successfully, add the search to the stack:
        previousSearches.add(new SearchStackItem(query));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                Log.d(TAG, "Home Pressed");//Never reached!
                if (!previousSearches.isEmpty()) {
                    SearchStackItem search = previousSearches.pop();
                    search(search.query, false);
                    return true;
                }           
                break;
            }
            default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.reddit_client, menu);
        MenuItem searchItem = menu.findItem(R.id.action_search);

        searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setQueryHint("Search user name...");
        searchView.setOnQueryTextListener(this);
        searchView.setOnSuggestionListener(this);
        searchView.setBackgroundColor(Color.BLACK);
        setSuggestionsEnabled(true);//sets up the suggestions cursor

        searchView.setQuery("", false);
        search(null, false);

    return super.onCreateOptionsMenu(menu);
    }
}

This is my menu XML that adds the SearchView to the Activity:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:rc="http://schemas.android.com/apk/res-auto" >

    <item android:id="@+id/action_search"
          android:title="@string/action_search"
          android:icon="@android:drawable/ic_menu_search"
          rc:showAsAction="ifRoom|collapseActionView"
          rc:actionViewClass="android.support.v7.widget.SearchView" />

</menu>

This approach makes a lot of sense logically, however when I click the Up button, the SearchView just closes, and I never see the output "Home Pressed". What do I need to do to get this to work correctly? I am looking for an answer that uses the compatibility ActionBar, not ActionBarSherlock, and not native ActionBar for higher APIs.


Things I have tried include Clicking app icon doesn't trigger onOptionsItemSelected() (following the comment in the answer regarding the support library), but this also did not do the trick.

I also stumbled upon the call getSupportActionBar().setHomeButtonEnabled(true), which also had no results. On doing some tests, it seems like clicking the up button registers no callback to onOptionsItemSelected.

1

There are 1 answers

0
Phil On BEST ANSWER

This was a simple mistake of not making the correct Stack calls.

previousSearches.add(new SearchStackItem(query));

Should have been

previousSearches.push(new SearchStackItem(query));