【问题标题】:Problem passing a Bundle with onSearchRequested使用 onSearchRequested 传递 Bundle 时出现问题
【发布时间】:2011-06-16 13:21:07
【问题描述】:

我实际上是在尝试使用 Android 的内置搜索界面,但是当我尝试使用搜索查询传递数据时遇到了一些问题。

这里有一个简短的解释:我在第一个 Activity (FirstActivity) 中有一个名为“Category”的对象,它实现了 Serializable(我已经在活动之间成功传递了它),我想执行与该类别相关的搜索,并显示结果在第二个Activity(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]

我尝试将其他一些对象添加到 Bundle(布尔值等)中,但它根本没有改变任何东西,而且我的 appData 一直为空。

【问题讨论】:

  • 请添加您的startSearch() 方法。问题可能出在这种方法中,您正在构建用于启动第二个活动的 Intent。
  • startSearch() 是来自 Activity 的现有方法:developer.android.com/reference/android/app/…。从我读到的,它应该可以处理那些东西。
  • 我发现最好的方法是重写 StartActivity。见:enjoysmile.com/blog/28/…

标签: android bundle android-searchmanager


【解决方案1】:

我也很难弄清楚这一点,而且我发现的例子并没有真正的帮助。他们中的很多人建议重写 onSearchRequested(),但这实际上不适用于 SearchWidget。我最终选择了using the following(来自 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);
}

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

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

简单,工作起来就像一个魅力!如果您想对此进行更多解释,请查看上面文章的链接。

【讨论】:

    【解决方案2】:

    如果您使用的是SearchView,它不会发送您的appData。相反,请考虑使用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 的额外部分是null,则表示您已经在侦听器中处理了搜索查询。

    结论:

    • 如果SearchView 处于非活动状态,而您通过onSearchRequested() 调用它,则会发生这种情况:onSearchRequested() >> onCreate() (ACTION_SEARCH 包含 SearchManager.APP_DATA)。
    • 如果SearchView 处于活动状态,用户键入并提交搜索,就会发生这种情况:SearchView.OnQueryTextListener.onQueryTextSubmit() >> onCreate() (ACTION_SEARCH 没有 SearchManager.APP_DATA)。

    【讨论】:

      【解决方案3】:

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

      试试

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

      谢谢 迪帕克

      【讨论】:

      • 感谢您的回答,但不幸的是,它并没有改变任何东西。正如我之前写的,intent.getExtras().keySet() 返回 [query, user_query] 所以,我的 Bundle 没有踪迹。
      【解决方案4】:

      在您的示例中,您要求的是原始意图对象上的键集,而不是包含您的 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));
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-03-15
        • 2020-07-14
        • 1970-01-01
        • 1970-01-01
        • 2023-01-22
        • 1970-01-01
        • 2011-09-12
        • 2013-04-07
        • 1970-01-01
        相关资源
        最近更新 更多