Two searchable.xml activities in one AndroidManifest.xml

孤街醉人 提交于 2019-12-03 10:09:35

In your Manifest file update the ImageListActivity activity tag

<activity
    android:name=".ImageListActivity"
    ...

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

So when you will trigger native search in ImageListActivity it will invoke the SearchResultsImageActivity and default one for others.

Assuming SearchResultsImageActivity is searchable.

JPM

One way I did this was to create fake activities then switch out the activities when you need them.

<activity android:name="activitySearchMain" />
  <activity android:name="activitySearchSub1">
    <intent-filter>
      <action android:name="android.intent.action.SEARCH" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.ALTERNATIVE" />
      <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
      <data android:scheme="user" />
    </intent-filter>
  </activity>
  <activity android:name="activitySearchSub2">
    <intent-filter>
      <action android:name="com.sample.twitter.action.SEARCH" />
      <category android:name="android.intent.category.DEFAULT" />
      <category android:name="android.intent.category.ALTERNATIVE" />
      <category android:name="android.intent.category.SELECTED_ALTERNATIVE" />
      <data android:scheme="user" />
    </intent-filter>
</activity>

Create two class that are named for the sub activities.

then create intents like this when component is clicked...

Intent sourceIntent = getIntent();
Intent newIntent = new Intent(this, activitySearchSub2.class);
newIntent.setAction(activitySearchSub2.ACTION2);
newIntent.setData(sourceIntent.getData());
startActivity(newIntent);
finish();

and call the intents from onClick when a button is clicked or some other component is click:

If you override the search function in just that activity, it should prevent the search call from going up to the application level. The return value controls if the call is propagated up.

@Override
public boolean onSearchRequested() {
    onPromptSearch();
    return false;  // don't go ahead and show the search box
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!