Search code examples
androidandroid-appcompatmaterial-designandroid-actionbar-compat

Android support library - SearchView in Toolbar does not call onSearchRequested


During changes from Holo to Material using Google support library I found an issue that it seems like onSearchRequested callback of Activity is not called. Both Activities - the one that starts search (ProjectDetail) and the one that is search target (ResultList) - are extending android.support.v7.app.ActionBarActivity.

Search works but it never reaches onSearchRequested callback therefore I cannot set additional parameters to search bundle.

There are many similar questions but none of the solutions worked for me so far, therefore I am asking here. I am fighting this for a couple of hours and maybe I am missing something terribly obvious as desperation is obscuring my vision :) Pretty please, help!

Snippet form Manifest:

<application>
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".ResultList" />

   <activity android:name="my.package.ProjectDetail">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".ResultList"/>
    </activity>

   <activity android:name="my.package.ResultList" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.gms.actions.SEARCH_ACTION" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
    </activity>
</application>

This is in onCreateOptionsMenu in Activity that starts the search:

getMenuInflater().inflate(R.menu.search, menu);

MenuItem searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

SearchableInfo info = searchManager.getSearchableInfo(getComponentName());

searchView.setSearchableInfo(info);
searchView.setIconifiedByDefault(true);
searchView.setQueryRefinementEnabled(true);
searchView.setQueryHint(getString(R.string.search_hint));

This is search.xml inflated in previous snippet, containing MenuItem and ActionView:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_search"
      android:title="@string/m_search"
      android:icon="@android:drawable/ic_menu_search"
      app:showAsAction="ifRoom|collapseActionView"
      app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

And this is onSearchRequested callback in ProjectDetail Activity:

@Override
public boolean onSearchRequested() {
        L.dd("SEARCH", "REQUESTED IN PROJECT");
        Bundle searchData = new Bundle();
        searchData.putLong(Const.EXTRA_PROJECT_ID, projectId);
        searchData.putLong(Const.EXTRA_ACCOUNT_ID, accountId);
        startSearch(null, false, searchData, false);
        return true;
}

Finally, this is searchable.xml as referenced from Manifest:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
            android:label="@string/app_label"
            android:hint="@string/search_hint" 
            android:searchSuggestAuthority="my.package.provider.SearchSuggestionsProvider"
            android:searchSuggestSelection=" ?"
            android:includeInGlobalSearch="true"
/>

Thanks in advance.


Solution

  • I had the same problem. Found one simple workaround here. Generally speaking you override method startActivity in search activity and put params to intent when action is equal to Intent.ACTION_SEARCH:

    @Override
    public void startActivity(Intent intent) {      
        // check if search intent
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            intent.putExtra("KEY", "VALUE");
        }
    
        super.startActivity(intent);
    }
    

    And then in searchable activity get passed params like this:

    private void handleIntent(Intent intent){
        if (Intent.ACTION_SEARCH.equals(intent.getAction())){
        mSearchedQuery = intent.getStringExtra(SearchManager.QUERY);
        mExtraData = intent.getStringExtra("KEY");
    }