Search code examples
androidandroid-intentsearchviewandroid-searchmanager

How to apply the search intent filter and android.app.searchable to all activities?


I have quite a few activities in my android app and need to implement an ActionBar with a SearchView.

I initially just added the following lines to my SearchActivity:

 <activity android:name=".activity.SearchActivity" android:launchMode="singleTop">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
  </activity>

However, searchManager.getSearchableInfo(getComponentName()); returned null in onCreateOptionsMenu when invoked from AnotherActivity1.

So I also added the intent-filter and android.app.searchable meta-data to AnotherActivity1 and that works for the AnotherActivity1 SearchView.

But I have many such activities - am I supposed to just copy-paste this XML snippet for all of them or is there some more elegant solution I'm missing?


Solution

  • Hmm, I think I figured it out myself.

    Use

    searchManager.getSearchableInfo(new ComponentName(this, SearchActivity.class));
    

    instead of

    searchManager.getSearchableInfo(getComponentName());
    

    Then I have to apply the intent-filter and meta-data only to SearchActivity.