Search code examples
androidandroid-intentandroid-searchandroid-searchmanager

Search intent not received by current activity


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!


Solution

  • After searching more, I found a solution to my problem.

    Keeping everything else same as I have posted above, I just updated my onCreateOptionsMenu(Menu menu) method in the Searchable activity (BrowseCategory in my case) with the code provided below:

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_filter, menu);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            SearchManager manager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
            SearchView search = (SearchView) menu.findItem(R.id.search).getActionView();
            search.setIconifiedByDefault(false);
            search.setSearchableInfo(manager.getSearchableInfo(getComponentName()));
            search.setOnQueryTextListener(new OnQueryTextListener() { 
                @Override 
                public boolean onQueryTextChange(String query) {
                    doSearch(query);
                    return true; 
                }
    
                @Override
                public boolean onQueryTextSubmit(String query) {
                    return false;
                }
            });
        }
        return true;
    }
    

    OnQueryTextListener is the key here! Adding this listeners triggers my search and now even pressing "Search" or "Go" on soft keyboard calls onNewIntent. About the later issue, reading from one of the answers in Cannot get searchview in actionbar to work I believe setting right SearchableInfo to searchview is important. Setting this is now triggering onNewIntent. If anyone knows more about it, please provide explanation. I will update if I find more on this too.