Does somebody know of a tutorial or an example of how to implement the standard Android search interface with Fragments? In other words, is it possible to put a standard search with a SearchManager in a Fragment?
Does somebody know of a tutorial or an example of how to implement the
Share
In short, you can’t. There are a couple of reasons why creating a search interface within a
Fragmentis not possible.When creating a searchable interface, you must specify a default “searchable activity” in your Android manifest. As I’m sure you know, a
Fragmentcannot exist without a parentActivityand thus, this separation is not possible.If you already figured out #1 already, I assume you asked this question in hopes that there is some magical “hack” out there that can get the job done. However, the documentation states that,
The underlying, internal system that is responsible for providing search results expects an
Activity, not aFragment; thus, implementing a search interface that is completely independent of anActivityis not possible, as it would require changes to the underlying system itself. Check out the source code for theSearchableInfoclass if you don’t believe me :).That being said, it doesn’t seem like it would be too difficult to achieve something similar to what you are describing. For instance, you might consider implementing your searchable-Activity so that it will accept the
android.intent.action.SEARCHintent and (instead of immediately displaying the results in aListView, for example) will pass the search query to yourFragments. For instance, consider the following searchable Activity:When a search-request is made, the system will launch your searchable activity, perform the query, and will pass the results to some container Activity (based on your implementation of
doMySearch). The container Activity will then pass these results to the contained searchableFragment, in which the results will be displayed. The implementation requires a bit more work than what you were probably hoping for, but I’m sure there are ways that you can make it more modular, and it seems like this might be the best that you can do.p.s. If you use this approach, you might have to pay special attention to which Activitys are added/removed to the backstack. See this post for some more information on how this might be done.
p.p.s. You might also forget about the standard search interface completely and just implement a simple search within a
Fragmentas described in Raghav’s post below.