Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSearchRequested() no longer works in Android 4.x+?

My app intercepts the search button to deliver something more relevant to the app itself:

  @Override
  public boolean onSearchRequested() {
    if (isOkToSearchWithinTheApp())
      doRelevantOwnSearch();
    return true; 
  }

It worked (still works) great on all Android versions except on my new 4.1.2 phone: On this Android 4.x phone, something called "Google Now" is invoked itself.

Is this a well known (i.e. documented change)?

If so, is there any other way to "intercept" the search button?

Update: I did exactly as suggested by @daniel_c05 below but touching the search button keeps yielding the same behavior: Bringing the Google search activity (and suspending my own activity, which is clearly not the desired behavior):

enter image description here

BTW, the accepted answer in this similar question is not acceptable... so, is there any other way to "intercept" the search button?

like image 396
Souper Avatar asked Dec 23 '12 20:12

Souper


2 Answers

Yup, Google hijacked the search button when it introduced Jelly Bean (Android 4.1) and if you try to intercept it earlier, in onKeyDown():

...
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
  return this.onSearchRequested();
}

You'll realize it won't work either.

But maybe this workaround could help you?

like image 145
ih8ie8 Avatar answered Nov 17 '22 00:11

ih8ie8


I have a Galaxy Nexus running 4.2.1 and I have no issues using oonSearchRequested

A few things to consider:

1 - Make sure you declare in your manifest that the searchable activity IS in fact searchable, and make sure you add the Search Intent Filter to catch onSearchRequested:

  <activity
        android:name="com.lazybits.rae.movil.Results"
        android:label="@string/title_activity_results"
        android:launchMode="singleTask"
        android:parentActivityName="com.lazybits.rae.movil.Home" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
    </activity>

2 - Override onKeyDown() to make a proper search interface:

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if ((keyCode == KeyEvent.KEYCODE_SEARCH) && isOkToSearchWithinTheApp) {
        onSearchRequested();
        return true;
        }
    return super.onKeyDown(keyCode, event);
    }

At this point if you have a searchWidget it should be loaded automatically, and you are now just waiting on user input. You would want to override onNewIntent for handling the search via Action.Search or Action.View depending on your searchable.xml behaviour.

EDIT

OK, can you do me a favor, and simply try to override onKeyDown, but not put any code in it, like this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {     

    if (keyCode == KeyEvent.KEYCODE_SEARCH) {
            //do nothing, simply return true to avoid regular behavious
            return true;
        }
    }

What I want to test, is to see if that will stop Google now from coming up, this should do absolutely nothing. If that works, then afterwards you can add your code, just make sure you return true (to avoid further propagation of the event), and that you don't call onSearchRequested.

like image 25
daniel_c05 Avatar answered Nov 17 '22 00:11

daniel_c05