简体   繁体   中英

Android - SearchView does not open Activity

I am trying out the tutorial to set up a SearchView explained here: https://developer.android.com/training/search/setup.html

I have created a menu with a SearchView & configured the SearchView. But my SearchResultActivity does not open on clicking the 'search' button on the keypad or the SearchView's submit button.

I also tried to follow Search widget on action bar doesn't trigger my search activity but I am unable to make the SearchView open the second Activity. Could somebody please help ?

Here is my menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.sample.searchbarapplication.MainActivity">
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:title="@string/action_search"
        android:icon="@drawable/ic_search_white_24dp"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|ifRoom" />
</menu>

The search-configuration 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" />

Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.searchbarapplication">

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

        <meta-data
            android:name="android.app.default_searchable"
            android:value=".MainActivity" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

        </activity>

        <activity android:name=".SearchResultActivity"
            android:launchMode="singleTop">

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

        </activity>

    </application>

</manifest>

MainActivity which has the SearchView in its toolbar:

public class MainActivity extends AppCompatActivity {
ActionMenuView mActionMenuView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

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

    searchView.setIconifiedByDefault(false);
    searchView.setSubmitButtonEnabled (true);

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_search) {
        Log.v("click","click");
    }

    return true;
}
}

And SearchResultActivity.java:

public class SearchResultActivity extends AppCompatActivity {

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

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        Log.v("query2","query");
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //use the query to search your data somehow
            Log.v("query",query.toString());
        }
    }
}

Edit - Here is my layout file for MainActivity that contains the SearchView. The contents of the include tag is an (empty) RelativeLayout with no child views.

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.sample.searchbarapplication.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>

Edit 2: When I bring

 <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

into the MainActivity's tag, the MainActivity is lauched again on a search (when the search button in the keypad or the search result button in the toolbar is pressed). But what I'd like to do is start a new Activity that is not MainActivity. Is this possible ?

My new manifest looks like this (notice the extra intent filter in MainActivty's tag):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.searchbarapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".MainActivity" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable" />

            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

        </activity>

        <activity android:name=".SearchResultActivity">

        </activity>

    </application>

</manifest>

This seems to be a common misunderstanding, I also had this problem the first time I wanted a new activity for the search results.

After re-checking the documentation here , I realized that the "SearchableActivity" is actually the one that will display the results.

So you will have to change this for the MainActivity:

<activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".SearchResultActivity"/>

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
</activity>

This for the SearchResultActivity:

<activity android:name=".SearchResultActivity">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
 </activity>

So summarised, the "android.app.default_searchable" meta-tag must be within the activity that starts the search . And the activity that will display the search needs the intent-filter and the meta-data for searchable.

Update the AndroidManifest.xml like the following

<activity
  android:name=".MainActivity"
  android:theme="@style/AppTheme.NoActionBar">

  <meta-data
    android:name="android.app.default_searchable"
    android:value=".SearchResultActivity"/>

  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER"/>
  </intent-filter>
</activity>

<activity android:name=".SearchResultActivity">
  <intent-filter>
    <action android:name="android.intent.action.SEARCH" />
  </intent-filter>

  <meta-data
    android:name="android.app.searchable"
    android:resource="@xml/searchable" />
</activity>

And in the onCreateOptionsMenu replace

searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

with

searchView.setSearchableInfo(
            searchManager.getSearchableInfo(new ComponentName(this,SearchResultActivity.class)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM