androidsearchandroid-manifestsearchactivity

onSearchRequested does not start search_activity


I want to start a search activity when clicking a button, right now the oncreate() is not being called. I suspect an error in my AndroidManifest.xml but can't find it..

Button click event within Main_Activity

public void search(View view) {

    Log.d("search", "start searchButton"); //this is shown in logcat
    onSearchRequested();
}

AndroidManifest.xml

    <!--Main activity.-->
    <activity
        android:name=".Main_Activity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <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=".Search_Activity" />
    </activity>

    <!--Activity and metadata for search activity. -->
    <activity
        android:name=".Search_Activity"
        android:label="@string/app_name"
        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>

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"
</searchable>

search_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/searchList" />
</LinearLayout>

Search_Activity.java

public class Search_Activity extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.d("search", "start search"); //does not show in logcat!!!!!!!!!!
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_layout);

    handleIntent(getIntent());

}

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

public void onListItemClick(ListView l,
                            View v, int position, long id) {
    // call detail activity for clicked entry
}

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

private void doSearch(String queryStr) {
    // get a Cursor, prepare the ListAdapter
    // and set it
}
}

does somebody see a mistake? or any thoughts on how i can find out what goes wrong?


Solution

  • You should start a new activity using Intent. Try this:

    public void search(View view) {
    
        Log.d("search", "start searchButton"); //this is shown in logcat
        startActivity(new Intent(this, Search_Activity.class));
    }