0

I'm trying to implement SearchWidget correctly, but I'm facing up with some problems. I override onSearchRequested() because I need to pass some information's from the calling Activity to the Activity that executes the query.

It happens only when the SearchWidget looses its "focus". This is what I mean for "focused" SearchWidget:
Focused SearchWidget
If I search without touching outside the SearchWidget (and so with the focuses SearchWidget) onSearchRequested() is correctly called. BUT when I touch outside the SearchWidget and it looses it focus, when I perform the search, the method onSearchRequested() is not called.
SearchWidget without focus

This is the code of onSearchRequested:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.actionbar_mainscreen, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    mSearchItem = menu.findItem(R.id.search);

    mSearchView = (SearchView) mSearchItem.getActionView();
    mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    mSearchView.setIconifiedByDefault(true);

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()){
        case R.id.search:
            onSearchRequested();
            break;
        case R.id.mainscreen_favorites:
            Intent intent = new Intent(this, FavoritesActivity.class);
            startActivity(intent);
            break;
    }
    return true;
}

@Override
public boolean onSearchRequested() {
    Bundle appData = new Bundle();
    appData.putInt(Constants.EXTRA_CATEGORY, index);
    startSearch(null, false, appData, false);
    return true;
}
Saurabh
  • 386
  • 3
  • 11
Gnufabio
  • 1,431
  • 4
  • 16
  • 26

2 Answers2

3

I ended up by starting my search activity from SearchView.OnQueryTextListener using a normal Intent.

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
      startSearch(query);
      return true;
    }

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

  private void startSearch(final String query) {
    // Doesn't call through onSearchRequest
    Intent intent = new Intent(this, SearchActivity.class);
    intent.putExtra(Keys.CATALOG, getCatalogID());
    intent.putExtra(Keys.HAS_ARTICLES, hasArticles);
    intent.putExtra(Keys.QUERY, query);
    startActivity(intent);
  }
Henrik Kirk
  • 595
  • 1
  • 4
  • 18
1

Try adding:

mSearchView.setFocusable(false);
David Scott
  • 1,656
  • 12
  • 22