onCreate() or onNewIntent() not fired when search

2019-02-28 21:48发布

问题:

I'm trying to implement search functionality in my app, which is very basic at the moment. When I press the search button on my Nexus, the search intent seems to not fire because neither onCreate() nor onNewIntent() gets called. I have basically copied the whole example you can find on Android developers, but it is still not working. Thanks for your help

res/searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="hello"
    android:hint="Search lectures" >
</searchable>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.openday.lectures"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LecturesListActivity"
                  android:label="@string/app_name"
                  android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />            
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
        </activity>
        <activity android:name=".SingleLectureActivity"
                  android:label="Lecture">
        </activity>
    </application>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

LecturesListActivity.java (shortened)

public class LecturesListActivity extends ListActivity {

    private String categoryDisplayed;
    private String facultyDisplayed;
    private List<Lecture> lectures;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadLectures();
        displayFaculties();
    }

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

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

回答1:

Problem with constant string in searchable.xml, same answer as this question