13

I've followed the SearchManager documentation yet am still having trouble making one of my app's activities searchable. From my activity, the Search dialog appears, I enter a query, hit search, my activity reopens, then I see this in the log:

D/SearchDialog(  584): launching Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=com.clinkybot.geodroid2/.views.Waypoints (has extras) }
I/SearchDialog(  584): Starting (as ourselves) #Intent;action=android.intent.action.SEARCH;launchFlags=0x10000000;component=com.clinkybot.geodroid2/.views.Waypoints;S.user_query=sdaf;S.query=sdaf;end
I/ActivityManager(  584): Starting activity: Intent { act=android.intent.action.SEARCH flg=0x10000000 cmp=com.clinkybot.geodroid2/.views.Waypoints (has extras) }
D/WAYPOINTS( 1018): NI Intent { cmp=com.clinkybot.geodroid2/.views.Waypoints (has extras) }
D/WAYPOINTS( 1018): NI null
D/WAYPOINTS( 1018): NI false 

It appears to me that everything is fine up until the last three lines. The "NI" lines are getIntent().toString(), getIntent().getAction(), and getIntent().hasExtra(SearchManager.QUERY) respectively.

ActivityManager appears to be starting my activity with the correct action. Then when my activity starts, it contains no action!? What am I doing wrong?

The relevant portion of my manifest is:

<activity android:name=".views.Waypoints" android:label="Waypoints" android:launchMode="singleTop">
   <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
    <category android:name="android.intent.category.DEFAULT" />
   </intent-filter>
   <meta-data android:name="android.app.searchable"
    android:resource="@xml/searchable" />
  </activity>
Jeremy Logan
  • 47,151
  • 38
  • 123
  • 143
originalbryan
  • 1,027
  • 1
  • 13
  • 21

1 Answers1

16

That took too many hours of my life. When performing a search from the searchable singleTop activity (in my case Waypoints) you must override onNewIntent() and grab the search query there. Which after a few hours in, I was doing. The catch was that getIntent() does not return the Intent used to call the activity (head explodes). It seems to return the original Intent that opened my searchable activity before I performed my first search.

The onNewIntent method receives the search intent. I replaced getIntent() with the param from onNewIntent() and boom, progress.

Though I must admit; figuring this out eases the frustration of being unable to escape the sound of Dancing with the Stars blaring in the background.

originalbryan
  • 1,027
  • 1
  • 13
  • 21
  • I'm glad I found this SO question before my head exploded as well. This is mentioned in the Application Fundamentals guide, but damn it would be nice to have mention of onNewIntent in [Intent#FLAG_ACTIVITY_SINGLE_TOP](http://j.mp/gkTnyw) instead of only in [Intent#FLAG_ACTIVITY_CLEAR_TOP](http://j.mp/fNlEM3). Furthermore, the documentation for [Activity#getIntent](http://j.mp/ftcS6p) doesn't even mention anything!!! Ugh... That said, thanks. – brack Jan 07 '11 at 18:51
  • 1
    So maybe it'd be a good practice to always call onNewIntent(getIntent()) from onCreate, just so you know it's following the same code path. – mclin Jan 26 '11 at 22:22
  • your problem is explained here http://developer.android.com/guide/topics/search/search-dialog.html#LifeCycle , however even after applying this, i still get null... – max4ever Jun 01 '12 at 10:56