Android onSearchRequested() 回调到调用活动 [英] Android onSearchRequested() callback to the calling activity

查看:21
本文介绍了Android onSearchRequested() 回调到调用活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MapActivity,它会在按下搜索按钮时显示 Android 搜索框.SearchManager 管理对话框,并将用户的查询传递给可搜索的活动,该活动搜索 SQLite 数据库并使用自定义适配器显示结果.

I have a MapActivity that will display the Android Search box when a search button is pressed. The SearchManager manages the dialog, and passes the user's query to a searchable activity, which searches an SQLite DB and displays the results using a custom adapter.

这很好用——我从显示的数据库中得到了正确的结果.

This works fine - and I'm getting the correct results from the DB displayed.

但是,当用户单击搜索结果时,我想做的是在地图上的 MapActivity 中显示结果.目前,这意味着启动一个新的 MapActivity,使用 Bundle 传递搜索结果.

However, what I want to do is display the result in the MapActivity on a map when the user clicks on a search result. Currently, this means starting a new MapActivity, passing the search result in using a Bundle.

我认为更简洁的方法是将搜索结果传递回原始活动,而不是开始一个新活动.目前,我的活动堆栈进入 MapAct -> SearchManager -> 搜索结果 -> 新 MapAct.这意味着在新 MapAct 中按返回"将返回查询结果,然后返回原始 MapAct.

I'd have thought the cleaner way was to pass the search result back to the original activity, rather than start a new one. At the moment, my activity stack goes MapAct -> SearchManager -> Search Result -> New MapAct. Which means pressing "back" from the new MapAct will go back to the query results, then to the original MapAct.

似乎在Search Result中,调用finish()不会导致在调用MapActivity中调用onActivityResult.

It seems that in the Search Result, calling finish() does not cause onActivityResult to be called in the calling MapActivity.

任何想法如何获取此回调并维护合理的活动堆栈?

Any ideas how to get this callback and maintain a sensible activity stack?

推荐答案

我一直在寻找这个确切问题的答案,终于找到了可行的方法.我必须使原始调用活动成为可搜索活动,因此清单中的条目如下所示:

I've been digging around for an answer to this exact question and finally found something that works. I had to make the original calling activity also the searchable activity, so my entry in the manifest looks like this:

<activity android:name=".BaseActivity"
          android:launchMode="singleTop">
   <!-- BaseActivity is also the searchable activity -->
   <intent-filter>
      <action android:name="android.intent.action.SEARCH" />
   </intent-filter>
   <meta-data android:name="android.app.searchable"
              android:resource="@xml/searchable"/>
   <!-- enable the base activity to send searches to itself -->
   <meta-data android:name="android.app.default_searchable"
              android:value=".BaseActivity" />
</activity>

然后不是在这个活动中搜索,而是手动 startActivityForResult 与真正的搜索活动,这将允许你 setResultfinish回到原来的调用活动.

And then instead of searching in this activity, manually startActivityForResult with the real search activity, which will then allow you to setResult and finish back to the original calling activity.

我在 在此处发布博文.

这篇关于Android onSearchRequested() 回调到调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆