Android Search Activity in Single Activity
Asked Answered
A

4

12

I am trying to make my app consist a SINGLE activity. This activity should be able to create a search and also receive a search. Unfortunately, I am getting a "double" search bar in my SearchView when I click on the search button in the action bar. I mean that there is a search bar (dark-- SearchView) that appears for a second in the action bar, and then a second one (white) appears OVER the action bar. Any help? What am I doing wrong? Sorry, this search thing is all new and confusing to me.

MainActivity (the only activity):

public class MainActivity extends ActionBarActivity {
Menu mMenu;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //getSupportActionBar().setDisplayShowTitleEnabled(false);
    setContentView(R.layout.activity_main);

    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    handleIntent(intent);
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String query = intent.getStringExtra(SearchManager.QUERY);
        //use the query to search your data somehow
    }
}

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

@SuppressLint("NewApi")
@Override
public boolean onSearchRequested() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        MenuItem mi = mMenu.findItem(R.id.search);
        if(mi.isActionViewExpanded()){
            mi.collapseActionView();
        } else{
            mi.expandActionView();
        }
    } else{
        //onOptionsItemSelected(mMenu.findItem(R.id.search));
    }
    return super.onSearchRequested();
}
}

main.xml (the menu xml):

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:com.brianco.andypedia="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/search"
      android:title="@string/action_settings"
      android:icon="@drawable/ic_launcher"
      android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
      com.brianco.andypedia:showAsAction="always|collapseActionView"
      com.brianco.andypedia:actionViewClass="android.support.v7.widget.SearchView" />

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

</menu>

searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="@string/app_name"
    android:hint="@string/search_hint"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer" />

in the manifest:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.AppCompat.Light.DarkActionBar" >
    <activity
        android:name="com.brianco.andypedia.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />
    </activity>

    <meta-data android:name="android.app.default_searchable"
        android:value=".MainActivity" />
Alexiaalexin answered 11/10, 2013 at 0:3 Comment(0)
A
3

Okay, the problem had to do with calling onSearchRequested() in onOptionsItemSelected(MenuItem item). That is redundant when I have a SearchView and should only be called on older platforms.

So, I created a separate menu item for devices under Honeycomb. It is removed at runtime for newer devices. The SearchView is removed at runtime for older devices.

See updated code below:

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        //remove old
        menu.removeItem(R.id.search_old);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    } else{
        //remove new
        menu.removeItem(R.id.search);
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search_old:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

@SuppressLint("NewApi")
@Override
public boolean onSearchRequested() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        MenuItem mi = mMenu.findItem(R.id.search);
        if(mi.isActionViewExpanded()){
            mi.collapseActionView();
        } else{
            mi.expandActionView();
        }
    } else{
        //onOptionsItemSelected(mMenu.findItem(R.id.search));
    }
    return super.onSearchRequested();
}
Alexiaalexin answered 11/10, 2013 at 1:11 Comment(0)
C
26

From Setting Up the Search Interface in the Android Documentation:

In your searchable activity, handle the ACTION_SEARCH intent by checking for it in your onCreate() method.

Note: If your searchable activity launches in single top mode (android:launchMode="singleTop"), also handle the ACTION_SEARCH intent in the onNewIntent() method. In single top mode, only one instance of your activity is created and subsequent calls to start your activity do not create a new activity on the stack. This launch mode is useful so users can perform searches from the same activity without creating a new activity instance every time.

Comb answered 10/2, 2014 at 15:33 Comment(1)
So what did the asker do wrong? He has the handling in onNewIntent() as well.Donia
G
16

Please try to add the following attribute to your <activity> in your manifest file:

android:launchMode="singleTop"

This will make the same activity to receive the search intent.

More info here: http://developer.android.com/guide/topics/manifest/activity-element.html

Also, you have <intent-filter> declared twice, you should merge it into one element.

Gerigerianna answered 11/10, 2013 at 0:32 Comment(3)
Thanks, I updated my manifest. That was not my problem, but it is really good to know. See my answer for my solution to the problem I was having.Alexiaalexin
Remember to use the onNewIntent() method to process your search query as explained by Phil.Zildjian
There's absolutely no problem having multiple intent filters.Nanete
A
3

Okay, the problem had to do with calling onSearchRequested() in onOptionsItemSelected(MenuItem item). That is redundant when I have a SearchView and should only be called on older platforms.

So, I created a separate menu item for devices under Honeycomb. It is removed at runtime for newer devices. The SearchView is removed at runtime for older devices.

See updated code below:

@SuppressLint("NewApi")
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mMenu = menu;
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);



    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        //remove old
        menu.removeItem(R.id.search_old);
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(false);
    } else{
        //remove new
        menu.removeItem(R.id.search);
    }


    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.search_old:
            onSearchRequested();
            return true;
        default:
            return false;
    }
}

@SuppressLint("NewApi")
@Override
public boolean onSearchRequested() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        MenuItem mi = mMenu.findItem(R.id.search);
        if(mi.isActionViewExpanded()){
            mi.collapseActionView();
        } else{
            mi.expandActionView();
        }
    } else{
        //onOptionsItemSelected(mMenu.findItem(R.id.search));
    }
    return super.onSearchRequested();
}
Alexiaalexin answered 11/10, 2013 at 1:11 Comment(0)
H
1

THANKS, This has worked for me.

Manifest:

        <activity
        android:name=".Buscar"
        android:configChanges="orientation|screenSize"
        android:label="@string/title_activity_buscar"
        android:launchMode="singleTop">

        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

Activity:

    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // getIntent() should always return the most recent
    setIntent(intent);

    query = intent.getStringExtra(SearchManager.QUERY);
    mysearch(query);

}
Hoggish answered 12/11, 2019 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.