在使用 Google 支持库从 Holo 更改为 Material 期间,我发现了一个问题,似乎未调用 Activity 的 onSearchRequested
回调。这两个 Activity - 开始搜索的 Activity (ProjectDetail
) 和作为搜索目标的 Activity (ResultList
) - 正在扩展 android.support.v7.app.ActionBarActivity
。
搜索有效,但它永远不会到达 onSearchRequested
回调,因此我无法为搜索包设置额外的参数。
有很多类似的问题,但到目前为止没有一个解决方案对我有用,因此我在这里问。我为此奋斗了几个小时,也许我遗漏了一些非常明显的东西,因为绝望模糊了我的视线:)非常感谢,帮助!
fragment 形式 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
在前面的代码 fragment 中膨胀的,包含 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>
这是 ProjectDetail
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;
}
最后,这是从 list 中引用的 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"
/>
提前致谢。
最佳答案
我遇到了同样的问题。找到一种简单的解决方法 here .
一般来说,您在搜索 Activity 中覆盖方法 startActivity
并在操作等于 Intent.ACTION_SEARCH 时将参数放入 Intent :
@Override
public void startActivity(Intent intent) {
// check if search intent
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
intent.putExtra("KEY", "VALUE");
}
super.startActivity(intent);
}
然后在可搜索 Activity 中获取传递的参数,如下所示:
private void handleIntent(Intent intent){
if (Intent.ACTION_SEARCH.equals(intent.getAction())){
mSearchedQuery = intent.getStringExtra(SearchManager.QUERY);
mExtraData = intent.getStringExtra("KEY");
}
关于Android 支持库 - 工具栏中的 SearchView 不调用 onSearchRequested,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26991594/