2

I am considering decoupling my search activity from the main activity but am stalled. The example below "Assumes current activity is the searchable activity".

How do you proceed if the current activity is not the searchable activity? What do I replace getComponentName() with?

From http://developer.android.com/guide/topics/search/search-dialog.html

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the options menu from XML
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.options_menu, menu);

  // Get the SearchView and set the searchable configuration
  SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
  SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
  // Assumes current activity is the searchable activity
  searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
  searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

  return true;
}
Harry HB
  • 77
  • 3
  • 6
  • See Sloy's answer here http://stackoverflow.com/questions/11699206/cannot-get-searchview-in-actionbar-to-work hope it helps. – SuShiS Apr 30 '14 at 07:33

1 Answers1

2

I know it is really late but I was having same problem and solution is to NOT use getComponentName()

Instead you should use:

// Get the SearchView and set the searchable configuration
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
ComponentName componentName = new ComponentName(this, SearchableActivity.class);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName));

Source: https://www.youtube.com/watch?v=9OWmnYPX1uc At: 3:50

Evren Yurtesen
  • 2,267
  • 1
  • 22
  • 40