首页 文章

使用launchMode =“SingleTop”创建搜索活动

提问于
浏览
0

我有一个来自搜索菜单项的SearchActivity,其代码如下:

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

manifest 上,搜索活动声明如下:

<activity
    android:name=".activity.Search"
    android:launchMode="singleTop"
    android:label="@string/app_name">

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

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

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

在Search活动的 onCreate()onNewIntent() 方法中,我处理传入的意图但是如果我记录查询,无论我更改查询多少次,它只显示启动搜索活动的第一个查询 .

这里有什么我想念的吗?

1 回答

  • 0

    尝试设置

    <activity
            android:name=".SearchableActivity"
            android:launchMode="singleInstance"
            android:screenOrientation="landscape"
            >
            <meta-data
                android:name="android.app.searchable"
                android:resource="@xml/searchable"/>
    
            <intent-filter>
                <action android:name="android.intent.action.SEARCH"/>
            </intent-filter>
        </activity>
    

    xml是

    <?xml version="1.0" encoding="utf-8"?>
    <searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:hint="@string/search_hint"
    android:label="@string/app_name" />
    

    这是一个有效的代码

相关问题