I am implementing an activity where I want to add a search bar. I want the current activity to receive the intent back. I followed couple of examples available and have added following code in my application:
AndroidManifest.xml
<activity
android:name=".search.BrowseCategory"
android:launchMode="singleTop"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
I added following in the AndroidManifest.xml under application tag:
<meta-data
android:name="android.app.default_searchable"
android:value=".search.BrowseCategory" />
Now in my BrowseCategory activity:
@Override
protected void onNewIntent(Intent intent) {
Log.d(getPackageName(), "Search intent received!");
setIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
// Do work using string
doSearch(query);
}
}
private void doSearch(String query) {...}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.search_filter, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch(item.getItemId())
{
case R.id.search:
startSearch("", false, null, false);
break;
}
return true;
}
Search Menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/search"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom"
android:title="@string/search"/>
</menu>
Searchable XML
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search"
android:hint="@string/search" >
</searchable>
With this, I am getting a search field in my action bar. However, my onNewIntent in BrowseCategory activity is not getting call on pressing enter or search icon in soft keyboard. Can someone please help me with this issue? What am I missing here? Thanks a lot!