Home » Android » android – SearchView in one activity, results in another one

android – SearchView in one activity, results in another one

Posted by: admin June 15, 2020 Leave a comment


My use case is the following: in activity A, I have an action bar with a collapsible SearchView. When the user gives her query and presses the ‘search’ button, I would like to show activity B with the results. I’m failing to do so, here is my code:


<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"

Activity A:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    return true;

AndroidManifest for Activity A and B:



            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>



    <activity android:name=".ActivityB">


            <action android:name="android.intent.action.SEARCH"/>




Howerver, no matter what I try, getSearchableInfo in A always returns null.

I have the feeling I’m not understanding something. The only way I can make getSearchableInfo() return not null is when I put the SEARCH intent filter and the searchable meta-data into activity A in the manifest. But then, when I press the ‘search’ button, another instance of activity A is started again on top of the current one, which definitely is not what I want. I guess I could add singleTop to A and handle the SEARCH action in onNewIntent and start B, but I generally don’t want A to be singleTop, I want the default. Or do I?

I tried adding this to A in the manifest (as suggested in some other threads):

        android:value=".ActivityB" >

What am I doing wrong?

EDIT: I replaced the ‘getComponentName()’ line in A with the following:

searchView.setSearchableInfo(searchManager.getSearchableInfo(new ComponentName(this, ActivityB.class)));

and it seems to work the way I want! Am I doing something wrong here, or is this the correct way? I’m a bit uncertain because it is not mentioned anywhere!

How to&Answers:

According to Android documentations, SearchManager.getSearchableInfo(componentName) takes one argument, which is:

componentName: The activity to get searchable information for

In your case you need ActivityB, so constructing a ComponentName that points to ActivityB is the correct way

new ComponentName(this, ActivityB.class)


            <action android:name="android.intent.action.SEARCH"/>

add this intent filter to activity A.

2.SearchView Menu

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.search).getActionView();


  <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >



3.Call this method from oncreate.

private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);

            Debug.e("Suggestion handle click", "call " + query);



4. if you are using Autosuggestion Listview implement searchView.setOnQueryTextListener ,setOnSuggestionListener and setSuggestionsAdapter.