Reputation: 1546
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
:
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.
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;
}
Upvotes: 0
Views: 2048
Reputation: 604
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);
}
Upvotes: 3