TrippinBilly TrippinBilly - 4 months ago 12
Android Question

How to send selected search suggestion data to a custom activity

I've been able to successfully implement a search widget experience to use a content provider that returns a cursor with row data. The custom search suggestions appear nicely in a list under the ActionBar search box as expected.

What I need to do is send the selected search suggestion to a custom activity (presumably in a bundle?) It seems so simple but I've not been able to figure it out.

Currently, this code will ask me what app I'd like to use to open the intent. I'd like to send the selected suggestion data to the "MainActivity" in the manifest listed further below.

Thanks in advance!

searchable.xml

<?xml version="1.0" encoding="utf-8"?>

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint"
android:searchSuggestAuthority="com.myapp.SearchProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestThreshold="2"
android:searchMode="queryRewriteFromText" >


</searchable>


the search activity

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);


Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
// Handle the normal search query case
android.util.Log.w("****", "in ACTION_SEARCH");
String query = intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
} else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
// Handle a suggestions click (because the suggestions all use ACTION_VIEW)
android.util.Log.w("****", "in ACTION_VIEW");
doView(intent);
}
}

private void doSearch(String query) {

android.util.Log.w("search query:", query);

}

private void doView(final Intent queryIntent) {
Uri uri = queryIntent.getData();
String action = queryIntent.getAction();
Intent i = new Intent(action);
i.setData(uri);
startActivity(i);
this.finish();
}
}


the search section of the manifest:

<activity
android:name="com.myapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

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

<activity android:name="com.myapp.SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>

<provider android:authorities="com.myapp.SearchProvider"
android:name="com.myapp.SearchProvider" />

<meta-data android:name="android.app.default_searchable"
android:value="com.myapp.SearchableActivity" />

Answer

Phew, I figured it out.

In my content provider, I have to add a column in the Matrix cursor called: SearchManager.SUGGEST_COLUMN_INTENT_EXTRA_DATA and put in the value of the suggestion.

In my search activity doView() method I can extract it with:

Bundle extras = queryIntent.getExtras();
String data = extras.getString(SearchManager.EXTRA_DATA_KEY);
android.util.Log.w("keySet =", extras.keySet().toString()); // this showed me the keys available
android.util.Log.w(SearchManager.EXTRA_DATA_KEY, data);

From here, I can pass that on to my custom activity/intent. There may be a better way, but this works!