views:

766

answers:

1

I have an activity handling search (ACTIVITY_1), which works perfectly when I use the search (via SEARCH button on the phone) within/from this activity.

However, when I use search from another activity (ACTIVITY_2..x) by implementing onNewIntent and forward the query string to my Search_Activity.class (ACTIVITY_1)

@Override
protected void onNewIntent(Intent intent) {
    Log.i(TAG, "onNewIntent()");

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
     Log.i(TAG, "===== Intent: ACTION_SEARCH =====");
     Intent myIntent = new Intent(getBaseContext(), Search_Activity.class);
     myIntent.setAction(Intent.ACTION_SEARCH);
     myIntent.putExtra(SearchManager.QUERY, intent.getStringExtra(SearchManager.QUERY));
     startActivity(myIntent);
    }

}

it always pauses ACTIVITY_2 first and then goes to onCreate() of ACTIVITY_2.

  • Why does it recreate my ACTIVITY_2 when it is already there and doesn't go to onNewIntent directly?
  • Is there another way I can forward search queries directly to ACTIVITY_1? For example via a setting in the Manifest.xml
  • Is it possible to generally forward all search queries automatically to ACTIVITY_1 without even implementing onNewIntent in all the other activities?

Currently I have to put an <intent-filter> in every single activity to "activate" my custom search there and forward the query then to the activity that handles search via the onNewIntent (as shown above).

<activity android:name=".Another_Activity"
    android:theme="@style/MyTheme">
    <intent-filter>
     <action android:name="android.intent.action.SEARCH" />
     <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
     android:resource="@xml/searchable" />
</activity>
+5  A: 

I'm not sure I understand the chain of events your describing, but here's how you need to configure your application in the case were ACTIVITY_1 is the search Activity you always want to launch from all your other Activities when the user presses the 'search' button.

Assuming that the search button works perfectly on Activity1, you just need to add a bit of glue meta-data to your application telling it that all your other Activities should use ACTIVITY_1 for searching, as shown in the manifest snippet below:

<application>
  <meta-data 
     android:name="android.app.default_searchable"
     android:value=".ACTIVITY_1" />

   <!-- All your activities, service, etc. -->

</application>

Using this, you should be able to remove the intent-filters from all but ACTIVITY_1, and you won't need to use the onNewIntent handler in any of your other Activities.

Reto Meier
Thanks a lot Reto. You know what? This is one of the things I read in the SearchManager documentation and had it from the first day on I tried to implement search. However, instead of my own search it always showed up the Google Web Search. And I finally figured out why: I just had a stupid typo in my android:value=".ACTIVITY_1", so Android never found the right Activity and therefor went straight to the web search. Thanks for forcing me to look at this peace of code again.
znq