sremer
sremer

Reputation: 450

Android search from searchable activity

I have an Activity that holds a list. Via the Android onSearchRequested() I implemented a search. The results are shown as a list with the same adapter in another Activity. Working fine so far.

Also, I want to be able to search from that second Activity showing the new results in the same list.

My AndroidManifest.xml for the two activities:

<activity android:name=".ListActivity" android:label="List">
    <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" />
</activity>

<activity android:name=".SearchActivity" android:label="Results">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH" />
    </intent-filter>
    <meta-data android:name="android.app.searchable" android:resource="@xml/search" android:value=".SearchActivity" />
</activity>

The SearchActivity's onResume():

@Override
protected void onResume() {
    super.onResume();
    Intent queryIntent = getIntent();
    String value = queryIntent.getStringExtra(SearchManager.QUERY);
    setView(value);
}

The setView() method does a foreach loop through all objects adding them to a result-array which is used for a new Adapter that the list shows.

ca = new CustomAdapter(this, R.layout.customadapter, resultArray);
list.setAdapter(pa);
list.invalidate();

When trying to search from the second Activity the search bar appears, I can enter my search value, send it - but the list doesn't change (and even the keyboard stays). What's missing?

Edit: Tried to make it easier to understand.

Upvotes: 1

Views: 2935

Answers (1)

sremer
sremer

Reputation: 450

Found that question which describes kind of the same problem.

Instead of overriding onResume() I have to override onNewIntent()

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    String value = intent.getStringExtra(SearchManager.QUERY);
    setView(value);
}

Upvotes: 4

Related Questions