1

I am following this relatively simple tutorial on how to manually start a SearchableActivity so that I can get a result back from it.

He is basically setting up the base Activity (Activity A) as the searchable activity (in the manifest), so that it receives the search intent and then manually launch real Search Activity (Activity B) with startActivityForResult,

After altering the manifests, he only has one block of code. My question:

Where do you put this code?

private void handleIntent(Intent intent) {
   // Get the intent, verify the action and get the query
   if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
     String query = intent.getStringExtra(SearchManager.QUERY);
     // manually launch the real search activity
     final Intent searchIntent = new Intent(getApplicationContext(),
           MySearchActivity.class);
     // add query to the Intent Extras
     searchIntent.putExtra(SearchManager.QUERY, query);
     startActivityForResult(searchIntent, ACTIVITY_REQUEST_CODE);
   }

Activity A or B? And where do you call it?

EXTRA INFO - How I am doing it currently - does this get cut out?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.menu_search);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);

} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    setupNewSearchView(searchItem, searchManager);
}

return true;

}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupNewSearchView(final MenuItem searchItem, SearchManager searchManager) {
    android.widget.SearchView searchView = (android.widget.SearchView) searchItem.getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(true);

}
TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259

1 Answers1

2

First make Activity(A) singleInstance/singleTop in manifest.xml android:launchMode="singleInstance|singleTop"
then use the onNewIntent(Intent intent) from Activity(A) to handleIntent(intent)

Edit:
ActivityA.java

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

Please read this tutorial to get full clear view of what happening

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
  • I am not sure I follow. Where does `onNewIntent()` get called? I'm assuming in `Activity` A. But where? When `Activity` A starts (onCreate)? Or where the `SearchManager` normally gets called? – TheLettuceMaster Jul 17 '13 at 22:40
  • 1
    when you search (after writing text in the searchView and pressed search button on keyboard) the Android system will start a new instance of the Activity(A) (new Activity(A) with a Intent with search action), but and because we used 'singleInstance' the onNewIntent function on Activity(A) will be called with the search action intent – JafarKhQ Jul 17 '13 at 22:49
  • Thanks for all of your help again. I understand what you are saying. But still unclear as to the "where". You said "It will be called with the search action intent". Right now, I handle this (I believe) in the `onCreateOptionsMenu`. I have added that code above to how I am doing this BEFORE the needed change. Is there where I need to call it? (and remove what I have already done then?) – TheLettuceMaster Jul 17 '13 at 22:55
  • Thanks, I got it. I see what is happening. I just wasn't passing the query on to the `SearchableActivity`. – TheLettuceMaster Jul 17 '13 at 23:42