3

I am completely stuck and have spent hours reading possible solutions to this. Simply can't get my search activity to start.

I have a main activity, from which I want the search button to start a search activity:

file MainApp.java

public class MainApp extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mainapplayout);
    }
    public boolean onCreateOptionsMenu(Menu m){
      getMenuInflater().inflate(R.menu.mainappmenu,m);
      return true;
    }
    public boolean onOptionsItemSelected(MenuItem m) {
      if(m.getItemId()==R.id.menu_search){
        return onSearchRequested(); // THIS IS CALLED, AND RETURNS TRUE
      }else return false;
    }
}

Neither pressing the UI button, nor the hardware search key, invokes the search. onSearchRequested() is definitely called, and returns true.

file AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest package="org.homphysiology.lists"
 android:versionCode="1" android:versionName="1.0"  
 xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8" />
<application 
             android:name="org.homphysiology.lists.MainApp"
                  ****REMOVED THIS LINE NOW, thanks arju, still not working tho
             android:icon="@drawable/ic_launcher">
    <activity android:name="org.homphysiology.lists.MainApp">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <meta-data
            android:name="android.app.default_searchable"
            android:value="org.homphysiology.lists.SearchActivity"
        />
    </activity>
    <activity android:name="org.homphysiology.lists.SearchActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
            <category android:name="android.intent.category.DEFAULT" />
                 ****REMOVED THIS LINE NOW, thanks tushar, still not working tho
        </intent-filter>
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"
        />
    </activity>
</application>
</manifest>

and I have the following:

File res/xml/searchable.xml :

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schamas.android.com/apk/res/android"
  android:label="@string/search_label"
  android:hint="@string/search_hint"
/>

These strings are in /res/values/strings.xml.

The search activity itself is simple, but is never created - I have a breakpoint in the constructor:

file SearchActivity.java:

public class SearchActivity extends ListActivity {
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setListAdapter(...)
  }
}

Can anyone spot any problem here? [compiling in eclipse Juno 4.2.1, SDK 21.0.1, testing on HTC desire Z] Thanks...


ADDENDUM

((SearchManager)getSystemService(Context.SEARCH_SERVICE))
  .getSearchableInfo(getComponentName())

returns null. Maybe this is just hammering home the fact that my SearchActivity hasn't been registered properly...


Modifying @arju's suggestion: I can start the Search activity "manually" by using

boolean onOptionsItemSelected(MenuItem m){
   startActivity(new Intent(this,SearchActivity.class));  
   return true;
}

but of course, this won't work with the hardware button, and moreover, it doesn't display the system search dialog.


I have also tried setting the search metadata for the whole application as opposed to the main activity:

<manifest>
  <application>
     <activity ...MainApp...        >  ...  </activity>
     <activity ...SearchActivity... >  ...  </activity>
     <meta-data
         android:name="android.app.default_searchable"
         android:value="org.homphysiology.lists.SearchActivity"
     />
  </application>
</manifest>

same problem! search activity is never instantiated.

2 Answers 2

1

The Android documentation says:

Note: The < intent-filter> does not need a < category> with the DEFAULT value (which you usually see in < activity> elements), because the system delivers the ACTION_SEARCH intent explicitly to your searchable activity, using its component name.

EDIT:

It's this:

<searchable xmlns:android="http://schamas.android.com/apk/res/android"

should be:

<searchable xmlns:android="http://schemas.android.com/apk/res/android"

Notice schamas vs schemas :)

5
  • Thanks! I have tried removing the <category name=DEFAULT>. No luck! same problem, nothing happens on search button, and nothing in LOGCAT either. Mar 24, 2013 at 10:33
  • 1
    Are you doing logging in onCreate of your SearchActivity or just using a breakpoint? Can you try logging just to be certain?
    – Tushar
    Mar 24, 2013 at 10:38
  • OK have tried doing logs everywhere, it definitely calls onSearchRequested() of the MainApp, but never instantiates the SearchActivity class... mystery? Mar 24, 2013 at 11:00
  • 1
    @SanjayManohar Can you join this discussion in the chat room? chat.stackoverflow.com/rooms/26799/help-sanjay
    – Tushar
    Mar 24, 2013 at 11:09
  • Thank you so much @Tushar, that was it! just 'schema'. Works perfectly now! Mar 24, 2013 at 12:38
1

Try this

Intent i=new Intent("android.intent.action.SEARCH");

startActivity(i);

6
  • Thanks!. OK I have removed that property now. But same problem! not launching search... Mar 24, 2013 at 10:02
  • What is your exact problem can u tell as simple Mar 24, 2013 at 10:07
  • The application executes fine. I want the SearchActivity to launch when either the menu option is selected, or the hardware search key is pressed. At the moment, absolutely nothing happens in both cases. :-( Mar 24, 2013 at 10:10
  • onsearchrequsted() is that method you want to execute? u r saying its executing fine and returning true . can u post the code for that method. Mar 24, 2013 at 10:13
  • I haven't touched the default onSearchRequested(). I believe it's part of android.app.Activity, and it's supposed to create the default search dialog specified in the manifest? The docs say it will return true if the search dialog is created (developer.android.com/reference/android/app/…). But my SearchActivity isn't created! Mar 24, 2013 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.