在搜索查看一个活动,导致一个又一个 [英] SearchView in one activity, results in another one
问题描述
我的使用案例如下:在活动A,我有一个可折叠的搜索查看一个操作栏。当用户给她查询和presses搜索按钮,我想的结果显示b活动。我不这样做,这是我的code:
My use case is the following: in activity A, I have an action bar with a collapsible SearchView. When the user gives her query and presses the 'search' button, I would like to show activity B with the results. I'm failing to do so, here is my code:
searchable.xml:
searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:hint="@string/action_search_hint"
android:label="@string/app_name"/>
活动答:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
AndroidManifest的活动A和B:
AndroidManifest for Activity A and B:
<activity
android:name=".ActivityA">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".ActivityB">
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
Howerver,不管我怎么努力,getSearchableInfo在一个始终返回null。
Howerver, no matter what I try, getSearchableInfo in A always returns null.
我有我不认识的东西的感觉。我可以让getSearchableInfo的唯一途径()返回NOT NULL是当我把搜索意图过滤器和搜索元数据到一个活动中的体现。但是,当我preSS的搜索按钮,一个活动的另一个实例再次启动对当前之一,它肯定是不是我想要的顶部。我想我可以添加singleTop A和处理onNewIntent的搜索行动,并启动B,但我一般不希望一个是singleTop,我希望默认。还是我?
I have the feeling I'm not understanding something. The only way I can make getSearchableInfo() return not null is when I put the SEARCH intent filter and the searchable meta-data into activity A in the manifest. But then, when I press the 'search' button, another instance of activity A is started again on top of the current one, which definitely is not what I want. I guess I could add singleTop to A and handle the SEARCH action in onNewIntent and start B, but I generally don't want A to be singleTop, I want the default. Or do I?
我试过在清单中添加此为A(在一些其他线程建议):
I tried adding this to A in the manifest (as suggested in some other threads):
<meta-data
android:name="android.app.default_searchable"
android:value=".ActivityB" >
</meta-data>
我在做什么错了?
What am I doing wrong?
编辑:我取代了'getComponentName()中带有以下行:
I replaced the 'getComponentName()' line in A with the following:
searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));
和它似乎工作就是我想要的!我在这里做得不对,或这是正确的方法是什么?我有点不确定,因为它没有提及任何地方!
and it seems to work the way I want! Am I doing something wrong here, or is this the correct way? I'm a bit uncertain because it is not mentioned anywhere!
推荐答案
据<一个href=\"http://developer.android.com/reference/android/app/SearchManager.html#getSearchableInfo(android.content.ComponentName)\">Android单证中, SearchManager.getSearchableInfo(组件名)
接受一个参数,它是:
According to Android documentations, SearchManager.getSearchableInfo(componentName)
takes one argument, which is:
组件名:活动获取搜索信息
componentName: The activity to get searchable information for
在你的情况你需要 ActivityB
,所以构建的组件名它指向 ActivityB
是正确的方法。
In your case you need ActivityB
, so constructing a ComponentName that points to ActivityB
is the correct way
new ComponentName(this, ActivityB.class)
这篇关于在搜索查看一个活动,导致一个又一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!