0

I have taken all day about this problem:

I have two activity MainActivity and SearchDaemon, I want to put the search interface the MainActivity and delegate the query request to SearchDaemon which will hold all the search request in my applicaiton(without a UI),this is my manifeset.xml:

     <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>

        <meta-data
                android:name="android.app.default_searchable"
                android:value=".SearchableActivity"/>
    </activity>
    <activity
            android:name=".search.SearchDaemon"
            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>

And the MainActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_normal, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.map_menu_search).getActionView();

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

But when I enter something in the searchview, and submit, I can not get into the SearchDaemon.

What's the problem?

Szymon
  • 42,577
  • 16
  • 96
  • 114
hguser
  • 35,079
  • 54
  • 159
  • 293

2 Answers2

2

You need to change the declaration in your manifest. Remove search related info from .MainActivity and add android:value to .search.SearchDaemon.

<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity 
    android:name=".search.SearchDaemon"
    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"
        android:value=".search.SearchDaemon" />
</activity>

Change the way you use setSearchableInfo:

searchView.setSearchableInfo(searchManager.getSearchableInfo(
        new ComponentName(getApplicationContext(), SearchDaemon.class)));
Szymon
  • 42,577
  • 16
  • 96
  • 114
  • See also my other answer: http://stackoverflow.com/questions/18832890/android-nullpointerexception-on-searchview-in-action-bar/18942838#18942838 – Szymon Mar 28 '14 at 09:46
  • Thank you,this works, but this make it seems that the `searchinfo` are bound with the result activity rather than the user input activity, isn't it? WTF!!! How about if I want to use the `SearchDaemon` to perform query request from different activity with different searchinfo? – hguser Mar 28 '14 at 09:53
  • As long as you have one activity (`SearchDaemon`) to receive results, I think it should be ok. It's all pretty convoluted and not well documented. Took me a while to get it going in my app. – Szymon Mar 28 '14 at 10:07
0

It looks like the metadata in your MainActivity block is incorrect - do you have a class called SearchableActivity?:

<activity android:name=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>

  <meta-data
    android:name="android.app.default_searchable"
    android:value=".SearchableActivity"/>
</activity>
<activity
  android:name=".search.SearchDaemon"
  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>

If you change it to SearchDaemon, it should work:

<activity android:name=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN"/>
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>

  <meta-data
    android:name="android.app.default_searchable"
    android:value=".search.SearchDaemon"/>
</activity>

If you're using an ActivityAlias to start your MainActivity, the meta-data needs to go in the activity-alias block. Alternatively, if you want your whole app to use .search.SearchDaemon, the metadata can live inside the application block.

See this commit on my project which demonstrates a working setup.

ataulm
  • 15,195
  • 7
  • 50
  • 92