Abishek Chozhan Abishek Chozhan - 7 months ago 24
Android Question

Why does SearchView require two activities?

I'm wondering why implementing Search on Android requires two activities, one to display the SearchView widget and another to complete the actual search?

For example, let's call the activity where the SearchView widget is housed, MainActivity, and the one that completes the search, SearchActivity.

Android documentation recommends that I send the search query to SearchActivity and then do my search there, but what if I want to show the search results on MainActivity itself? Can I just implement handleIntent() on MainActivity?


Yes, you can certainly implement it in the same activity.

As per the Providing Search with SearchView video, you can use the setOnQueryTextListener() method to directly listen for text changes or when the user submits the search (skipping the need for a searchable configuration at all).

If you do want to use the searchable configuration, make sure your activity uses android:launchMode="singleTop" as per later in the same video. This prevents many copies of the activity from being created on top of one another.