android - 按下搜索按钮时未触发 onCreate() 或 onNewIntent()

标签 android search android-intent

我正在尝试在我的应用程序中实现搜索功能,目前这还很基础。当我按下 Nexus 上的搜索按钮时,搜索 Intent 似乎没有触发,因为 onCreate() 和 onNewIntent() 都没有被调用。我基本上复制了您可以在 Android developers 上找到的整个示例, 但它仍然无法正常工作。感谢您的帮助

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(缩短)

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);
        }
    }
}

最佳答案

searchable.xml 中的常量字符串有问题,答案与 this question 相同

关于android - 按下搜索按钮时未触发 onCreate() 或 onNewIntent(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5163132/

相关文章:

android - 有类似 "ON DELETE NULL"的东西吗?

android - Retrofit2 - 全局检查响应代码

android - Broadcast Receiver 每 5 分钟获取一次 wifi 信号

javascript - 使用元素 id 单击页面其他部分时隐藏 div

android - 如何在应用程序运行时持续检查互联网连接

c# - 从 Android KSoap2 使用在 Mono 上运行的 WCF Soap 服务

django - 在 Django 应用程序的数据库列中保存标签(逗号分隔)会使大数据库变慢

java - 如何在我的自定义 ListView 中使用搜索功能(从网站获取数据并在 ListView 中显示)

android - 将用户选择值从一个 Activity 传递到另一个 Activity

android - 使用 Intent 时不显示电子邮件正文