SearchView doesn't start Search Activity but searchbar opens
15:17 18 Oct 2015

I'm having trouble creating the search feature for my application. I have followed various tutorials, followed the Android Docs, and other Stack Overflow answers with no success. I have the icon for the each in one activity (ContinentActivity.java) and when clicked the toolbar opens up a search window. However typing data does not register in the SearchResultsActivity. It is never created. Could this be because I am using a toolbar and a menu?

AndroidManifest.xml




    
    

    

        

        

            
            
                             
            
            
        

        
            
                
                
            
        

        
            
        
        
            
        

        
        
            
        
            
                
            
                
                    
                
    

SearchResultsActivity.java

    public class SearchResultsActivity extends AppCompatActivity {
    public static final String TAG = "SEARCH_RESULTS_ACTIVITY";
    SearchResultsAdapter mSearchResultsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.v(TAG, "onCreate");
        handleIntent(getIntent());
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.v(TAG, "onNewIntent");
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.v(TAG, "HANDLE INTENT");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            showResults(query);
        }
    }

    private void showResults(String query) {
        // Query your data set and show results
        // ...
        Log.v(TAG, "Searching for " + query + "...");
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        Log.v(TAG, "onCreateOptionsMenu");
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_location, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo( searchManager.getSearchableInfo(getComponentName()) );

        return true;
    }

This activities menu has the search icon ContinentActivity.java

    public class ContinentActivity extends AppCompatActivity {

    private ArrayList mContinents;
    @Bind(R.id.recyclerView) RecyclerView mRecyclerView;
    @Bind(R.id.toolBar) Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location_selection);
        ButterKnife.bind(this);

        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        LocationDataSource dataSource = new LocationDataSource(this);
        dataSource.test();
        mContinents = dataSource.readContinents();

        ContinentAdapter adapter = new ContinentAdapter(this, mContinents);
        mRecyclerView.setAdapter(adapter);

        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setHasFixedSize(true);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_location, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        return super.onOptionsItemSelected(item);
    }
    }

menu_location.xml

    
    

        

    

tool_bar.xml

     
     

    
java android searchview