开发者

Two searchable.xml activities in one AndroidManifest.xml

I have an Android app which has a few different activities for browsing articles and images downloaded from RSS.

I'd like to be able to offer to hook up the search button to the Search dialog, using the a searchable.xml file. I've managed to do this with one search, using:

<activity android:name=".search.SearchResultsActivity"
    android:label="@string/search_results_activity_title" >
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable_articles"/>
</activity>

and in the <application />

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

I can now launch the Search dialog from any activity, and it launches the SearchResultsActivity.

I would now like to be able to search for images when the user is an ImageListActivity, using a searchable_images.xml, and use the default everywhere else.

I have a SearchResultsImageActivity which includes the following meta-data element, and used the same element in the 开发者_C百科ImageListActivity.

<meta-data android:name="android.app.searchable"
    android:resource="@xml/searchable_images"/>

On pressing the search button in the ImageListActivity, I get the default search from searchable_articles.xml.

If I change the default_searchable to SearchResultsImageActivity, the image search is always launched, and the article search is never launched.

If I remove the default_searchable meta-data element altogether, and add searchable meta-data only selected activities, no search is launched.

I'm fairly sure this should be possible, but I don't know what I'm doing wrong.


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.


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜