使用 onSearchRequested 传递 Bundle 时出现问题

发布于 2024-11-16 03:32:29 字数 1180 浏览 5 评论 0原文

我实际上正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

颜漓半夏 2024-11-23 03:32:29

我在解决这个问题时也遇到了问题,而且我发现的例子并没有真正的帮助。他们中的很多人建议重写 onSearchRequested(),但这实际上对 SearchWidget 不起作用。我最终使用以下(来自 danada)作为解决方案,因为对我来说它似乎比设置 OnQueryTextListener 简单得多。我只是像这样覆盖了startActivity(在第一个搜索活动中):

@Override
public void startActivity(Intent intent) {      
    //check if search intent
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
        intent.putExtra("KEY", "VALUE");
    }

    super.startActivity(intent);
}

然后在第二个可搜索活动中,我像这样提取了信息(从onCreate()或覆盖onNewIntent()(如果使用singleTop)调用):

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

简单,并且工作得非常有魅力!如果您想了解更多说明,请查看上面文章的链接。

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:

@Override
public void startActivity(Intent intent) {      
    //check if search intent
    if(Intent.ACTION_SEARCH.equals(intent.getAction())) {
        intent.putExtra("KEY", "VALUE");
    }

    super.startActivity(intent);
}

Then in the second, searchable Activity, I pulled out the info like so (called from onCreate() or from overriding onNewIntent() (if using singleTop)):

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

Simple, and worked like a charm! Check the link to the article above if you would like a little more explanation about it.

诗化ㄋ丶相逢 2024-11-23 03:32:29

如果您使用 SearchView,它不会发送您的 应用程序数据。相反,请考虑使用 OnQueryTextListener。例如:

...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your-menu-id, menu);

    /*
     * Get the SearchView and set the searchable configuration.
     */
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.your-search-menuitem-id)
            .getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    /*
     * Set query text listener here.
     */
    searchView.setOnQueryTextListener(mSearchViewOnQueryTextListener);

    return true;
}// onCreateOptionsMenu()

...
private final SearchView.OnQueryTextListener mSearchViewOnQueryTextListener = new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        /*
         * You don't need to deal with "appData", because you already
         * have the search query here.
         */

        // Tell the SearchView that we handled the query.
        return true;
    }// onQueryTextSubmit()

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }// onQueryTextChange()
};// mSearchViewOnQueryTextListener

注意:您仍然需要保留旧方法(在 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:

...
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your-menu-id, menu);

    /*
     * Get the SearchView and set the searchable configuration.
     */
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.your-search-menuitem-id)
            .getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    /*
     * Set query text listener here.
     */
    searchView.setOnQueryTextListener(mSearchViewOnQueryTextListener);

    return true;
}// onCreateOptionsMenu()

...
private final SearchView.OnQueryTextListener mSearchViewOnQueryTextListener = new SearchView.OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        /*
         * You don't need to deal with "appData", because you already
         * have the search query here.
         */

        // Tell the SearchView that we handled the query.
        return true;
    }// onQueryTextSubmit()

    @Override
    public boolean onQueryTextChange(String newText) {
        // TODO Auto-generated method stub
        return false;
    }// onQueryTextChange()
};// mSearchViewOnQueryTextListener

Note: You still need to keep the old way (using appData inside onSearchRequested()). In your onCreate(), if the extra for SearchManager.APP_DATA is null, that means you already handled the search query in the listener.

Conclusion:

  • If the SearchView is inactive, and you invoke it via onSearchRequested(), this will happen: onSearchRequested() >> onCreate() (ACTION_SEARCH contains SearchManager.APP_DATA).
  • If the SearchView is active, the user types and submits search, this will happen: SearchView.OnQueryTextListener.onQueryTextSubmit() >> onCreate() (ACTION_SEARCH without SearchManager.APP_DATA).
盛装女皇 2024-11-23 03:32:29

在放入和检索数据时,您使用两个不同的密钥。放置时您使用的是 "category" ,检索时您使用的是 SearchManager.APP_DATA 而不是使用 "category"

尝试一下,

Bundle appData = intent.getBundleExtra("category");

谢谢
迪帕克

While putting data and retrieving it you are using two different keys. while putting you are using "category" and while retrieving you are using SearchManager.APP_DATA instead of using "category"

Try with

Bundle appData = intent.getBundleExtra("category");

Thanks
Deepak

迷爱 2024-11-23 03:32:29

在您的示例中,您要求提供原始意图对象上的键集,而不是包含您的 appData 的 Bundle。这是一个应该有效的示例:

private void handleIntent(Intent intent){
    final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);

    for (String key : appData.keySet()) {
        Log.d(TAG, "key="+appData.getString(key));
    }
}

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:

private void handleIntent(Intent intent){
    final Bundle appData = intent.getBundleExtra(SearchManager.APP_DATA);

    for (String key : appData.keySet()) {
        Log.d(TAG, "key="+appData.getString(key));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文