onCreate() or onNewIntent() not fired when search button pressed

血红的双手。 提交于 2020-01-11 12:50:28

问题


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



来源:https://stackoverflow.com/questions/5163132/oncreate-or-onnewintent-not-fired-when-search-button-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!