【问题标题】:Android support library - SearchView in Toolbar does not call onSearchRequestedAndroid 支持库 - Toolbar 中的 SearchView 不调用 onSearchRequested
【发布时间】:2015-01-15 11:32:17
【问题描述】:

在使用 Google 支持库从 Holo 更改为 Material 期间,我发现了一个问题,似乎没有调用 Activity 的 onSearchRequested 回调。两个活动 - 开始搜索的活动 (ProjectDetail) 和作为搜索目标的活动 (ResultList) - 都在扩展 android.support.v7.app.ActionBarActivity

搜索有效,但它永远不会到达onSearchRequested 回调,因此我无法为搜索包设置其他参数。

有很多类似的问题,但到目前为止没有一个解决方案对我有用,因此我在这里问。我为此奋斗了几个小时,也许我错过了一些非常明显的东西,因为绝望正在遮蔽我的视线:) 请帮忙!

片段表单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>

这是在启动搜索的 Activity 中的 onCreateOptionsMenu

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));

这是search.xml在之前的sn-p中膨胀的,包含MenuItem和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>

这是onSearchRequested Activity 中的onSearchRequested 回调:

@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;
}

最后,这是清单中引用的searchable.xml

<?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"
/>

提前致谢。

【问题讨论】:

    标签: android android-appcompat material-design android-actionbar-compat


    【解决方案1】:

    我遇到了同样的问题。找到了一种简单的解决方法here。 一般来说,您在搜索活动中覆盖方法 startActivity 并在操作等于 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);
    }
    

    然后在可搜索的活动中获得这样的参数:

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

    【讨论】:

    • 谢谢,重写 startActivity 为我做了。然而,handleIntent 没有被调用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 2016-05-20
    相关资源
    最近更新 更多