使用 onSearchRequested 传递 Bundle 时出现问题
我实际上正在尝试使用 Android 的内置搜索界面,但是当我尝试使用搜索查询传递数据时遇到一些问题。
这是一个简短的解释:我在第一个活动(FirstActivity)中有一个名为“类别”的对象,它实现了可序列化(我已经在活动之间成功传递了它),我想执行与该类别相关的搜索,并将结果显示在第二个活动(SecondActivity)。
因此,在 FirstActivity 中,我重写了 onSearchRequest 方法:
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putSerializable("category", _currentCategory);
Log.d(Utils.LOG_TAG, "Bundle : "+appData.keySet());
startSearch(null, false, appData, false);
return true;
}
在 SecondActivity 中,我尝试获取此 Bundle:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
handleIntent(getIntent());
}
private void handleIntent(Intent intent){
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData == null) Log.d(Utils.LOG_TAG, "appData == null");
Log.d(Utils.LOG_TAG, "Extras : "+intent.getExtras().keySet());
}
问题是 appData 似乎每次都等于 null。这是 logcat 输出:
Bundle : [category]
appData == null
Extras : [query, user_query]
我尝试将一些其他对象添加到捆绑包中(布尔值等),但它根本没有改变任何内容,并且我仍然有一个空的 appData。
I'm actually trying to use the built-in search interface of Android, but I have some issues when I try to pass data with the search query.
Here is a brief explanation : I have an object in a first Activity (FirstActivity) called "Category" which implements Serializable (I already pass it successfuly between Activities) and I want to perform a search related to that category, and display the results in a second Activity (SecondActivity).
So, in FirstActivity I override the onSearchRequest method :
@Override
public boolean onSearchRequested() {
Bundle appData = new Bundle();
appData.putSerializable("category", _currentCategory);
Log.d(Utils.LOG_TAG, "Bundle : "+appData.keySet());
startSearch(null, false, appData, false);
return true;
}
And in SecondActivity, I try to get this Bundle :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
handleIntent(getIntent());
}
private void handleIntent(Intent intent){
Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);
if(appData == null) Log.d(Utils.LOG_TAG, "appData == null");
Log.d(Utils.LOG_TAG, "Extras : "+intent.getExtras().keySet());
}
Problem is that appData seems to be equals to null everytime. Here is the logcat output :
Bundle : [category]
appData == null
Extras : [query, user_query]
I tried to add some other objects into the Bundle (Booleans, etc...) but it doesn't change anything at all and I keep having a null appData.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我在解决这个问题时也遇到了问题,而且我发现的例子并没有真正的帮助。他们中的很多人建议重写 onSearchRequested(),但这实际上对 SearchWidget 不起作用。我最终使用以下(来自 danada)作为解决方案,因为对我来说它似乎比设置 OnQueryTextListener 简单得多。我只是像这样覆盖了startActivity(在第一个搜索活动中):
然后在第二个可搜索活动中,我像这样提取了信息(从onCreate()或覆盖onNewIntent()(如果使用singleTop)调用):
简单,并且工作得非常有魅力!如果您想了解更多说明,请查看上面文章的链接。
I had problems figuring this out as well, and the examples I found didn't really help. A lot of them suggested overriding onSearchRequested(), but that actually doesn't work for SearchWidget. I ended up using the following (from danada) as a solution, since it seemed much simpler for me than setting up the OnQueryTextListener. I just overrode startActivity (in the first, search Activity) like so:
Then in the second, searchable Activity, I pulled out the info like so (called from onCreate() or from overriding onNewIntent() (if using singleTop)):
Simple, and worked like a charm! Check the link to the article above if you would like a little more explanation about it.
如果您使用 SearchView,它不会发送您的
应用程序数据
。相反,请考虑使用 OnQueryTextListener。例如:注意:您仍然需要保留旧方法(在
onSearchRequested()
内使用appData
)。在onCreate()
中,如果SearchManager.APP_DATA
的 extra 为null
,则意味着您已经在侦听器中处理了搜索查询。结论:
SearchView
处于非活动状态,并且您通过onSearchRequested()
调用它,则会发生以下情况:<代码>onSearchRequested()>>onCreate()
(ACTION_SEARCH
包含SearchManager.APP_DATA
)。SearchView
处于活动状态,用户键入并提交搜索,则会发生以下情况:SearchView.OnQueryTextListener.onQueryTextSubmit()
>>>onCreate()
(ACTION_SEARCH
不带SearchManager.APP_DATA
)。If you're using SearchView, it will not send your
appData
. Instead, consider using OnQueryTextListener. For example:Note: You still need to keep the old way (using
appData
insideonSearchRequested()
). In youronCreate()
, if the extra forSearchManager.APP_DATA
isnull
, that means you already handled the search query in the listener.Conclusion:
SearchView
is inactive, and you invoke it viaonSearchRequested()
, this will happen:onSearchRequested()
>>onCreate()
(ACTION_SEARCH
containsSearchManager.APP_DATA
).SearchView
is active, the user types and submits search, this will happen:SearchView.OnQueryTextListener.onQueryTextSubmit()
>>onCreate()
(ACTION_SEARCH
withoutSearchManager.APP_DATA
).在放入和检索数据时,您使用两个不同的密钥。放置时您使用的是
"category"
,检索时您使用的是SearchManager.APP_DATA
而不是使用"category"
尝试一下,
谢谢
迪帕克
While putting data and retrieving it you are using two different keys. while putting you are using
"category"
and while retrieving you are usingSearchManager.APP_DATA
instead of using"category"
Try with
Thanks
Deepak
在您的示例中,您要求提供原始意图对象上的键集,而不是包含您的 appData 的 Bundle。这是一个应该有效的示例:
In your example, you are asking for the keyset on the original intent object, and not the Bundle containing your appData. Here is an example that should work: