Search code examples
androidsearchviewandroid-search

SearchView does not trigger SearchActivity


I am having trouble making my SearchView in the ActionBar trigger an intent that is received by my SearchActivity. I think the issue is in the Manifest/XML somewhere, because the logcat isn't even showing the log in the onCreate method of the SearchActivity. I've used these resources, and others (including SO responses), to try and implement it, but haven't had any luck.

manifest:

  <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".activitiesFragments.MainActivity"
        android:configChanges="orientation"
        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=".activtiesFragments.SearchActivity"/>

    </activity>

    <activity
        android:name=".activitiesFragments.LoginActivity"
        android:label="@string/login_button_text"
        android:screenOrientation="portrait" />

    <activity
        android:name=".activitiesFragments.SignUpActivity"
        android:label="@string/create_account_button"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".activitiesFragments.LoginActivity" />
    </activity>

    <activity android:name=".activitiesFragments.SearchActivity">
        <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>

</application>

MainActivity:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_messages, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (android.support.v7.widget.SearchView) menu.findItem(R.id.friends_menu_item).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

    return true;
}

SearchActivity

package dev.workman.shoutout.activitiesFragments;

import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;

import dev.workman.shoutout.R;

public class SearchActivity extends AppCompatActivity {

    ArrayList<String> query_results = new ArrayList<>();
    ArrayAdapter<String> adapter;
    ListView results_view;

    String TAG = "SearchActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        results_view = (ListView) findViewById(R.id.results_list);
        adapter = new ArrayAdapter<>(this, android.R.layout.simple_expandable_list_item_1, query_results);
        results_view.setAdapter(adapter);

        Log.d(TAG, "onCreate: created searchview activity");

        handle_intent(getIntent());

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        query_results.clear();
        handle_intent(getIntent());
    }

    public void handle_intent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            Log.d(TAG, "handle_intent: query " + query);
            // process the query, add values to the query_results adpater
            // then call adapter.notifyDataSetChanged()
        }
    }

}

Solution

  • I figured out my problem. Rather than an XML/manifest issue, I the problem lied with the getComponentName() call.

    I changed it to

    new ComponentName(MainActivity.this, SearchActivity.class)
    

    and it works now. I hope this helps others with the same issue