Search code examples
androidandroid-searchmanager

Search Interface opens a new Activity when search


I am trying to use the Search Manager from Android.

In fact, I have an activity where I am calling

 onSearchRequested()

Then, I am using in the same activity this function to get the search string:

 // Get the intent, verify the action and get the query
        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
        }

The problem is the next: When i am clicking to the search button, I am opening a new activity and i would like to stay on the same and do some searches. So, my goal is to avoid the new activity opening when I click to the search button.

Thank you.


Solution

  • The solution consists to reuse the developpers example http://developer.android.com/guide/topics/search/search-dialog.html

    The important thing is to create 2 activities, one for searches and one other for displaying the results.

    In the manifest, use this code:

    <application ... >
    <!-- this is the searchable activity; it performs searches -->
    <activity android:name=".SearchableActivity" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
                   android:resource="@xml/searchable"/>
    </activity>
    
    <!-- this activity enables the search dialog to initiate searches
         in the SearchableActivity -->
    <activity android:name=".OtherActivity" ... >
        <!-- enable the search dialog to send searches to SearchableActivity -->
        <meta-data android:name="android.app.default_searchable"
                   android:value=".SearchableActivity" />
    </activity>
    ...