Android support library - SearchView on toolbar doesn't call onSearchRequested

During the changes from Holo to Material using the google support library, I found that the onSearchRequested

callback Activity is not being called. Both operations, the one that starts the search ( ProjectDetail

) and the one that is the target of the search ( ResultList

), are expanded android.support.v7.app.ActionBarActivity

.

The search works, but it never reaches the callback onSearchRequested

, so I cannot set additional parameters for the search.

There are many similar questions, but none of the solutions have worked so far, so I am asking here. I've been struggling with this for hours and maybe I'm missing something terribly obvious as despair obscures my vision :) Enough please help!

Fragment shape 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 the value onCreateOptionsMenu

in action that triggers 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

overstated in the previous snippet, containing a MenuItem and an 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 the onSearchRequested

callback to 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 stated in the 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.

+3


source to share


1 answer


I had the same problem. Found one simple solution here . Generally speaking, you override a method startActivity

in a search activity and put parameters into an intent when the activity 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 the search operation, pass parameters like this:

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

      

+2


source







All Articles