SearchView会无意启动可搜索的活动 [英] SearchView launches searchable activity without intent

查看:145
本文介绍了SearchView会无意启动可搜索的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SearchView来启动一个显示搜索结果的新活动.我遵循以下来源:

I am using a SearchView for starting a new activity that shows the search results. I have followed the following sources:

  • Android tutorial: Creating a Search Interface
  • SO: Start new activity from SearchView
  • SO: Cannot get searchview in actionbar to work

新的可搜索活动ListActivity是从MainActivity的应用栏中的SearchView小部件启动的.新的可搜索活动已启动,但是缺少搜索意图(从未调用onNewIntent方法).

The new searchable activity ListActivity is launched from a SearchView widget inside the App Bar in MainActivity. The new searchable activity is started but the search intent is missing (onNewIntent method is never called).

Searchable.xml

Searchable.xml

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

AndroidManifest.xml

AndroidManifest.xml

<application
    ...
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".ui.ListActivity" />
    <activity
        android:name=".ui.MainActivity"
        ...
    </activity>
    <activity
        android:name=".ui.ListActivity"
        android:launchMode="singleTop"
        ...
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable" />
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
    </activity>
</application>

MainActivity

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // setSupportActionBar
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main_menu, 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); 
        return true;
    }
}

ListActivity

ListActivity

public class ListActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {    
        Log.d(TAG, "onCreate invoked");    //Log Printed
        ...
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.d(TAG, "onNewIntent invoked");    //Log NOT Printed
    }
}

考虑到我也用new ComponentName(this, ListActivity.class)替换了getComponentName(),但是得到了相同的结果:没有错误,没有意图查询.

Consider that I have also replaced getComponentName() with new ComponentName(this, ListActivity.class) but got the same result: no errors, no intent query.

推荐答案

按照

对于在其程序包中将launchMode设置为"singleTop"的活动,或者客户端使用 FLAG_ACTIVITY_SINGLE_TOP 标志rel ="nofollow"> startActivity(Intent).无论哪种情况,当在活动堆栈的顶部重新启动活动而不是启动活动的新实例时,将在现有实例上使用用于重新启动的Intent调用onNewIntent().它.

This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

singleTop仅在活动(在您的情况下为ListActivity)位于顶部时才适用-代替创建新实例,它将重用现有实例.但是,如果您仅从MainActivity中进行搜索(搜索之后在ListActivity上击中),那么您将销毁ListActivity实例,然后创建一个新实例-导致调用onCreate(),而不是onNewIntent().

singleTop only applies if the activity (in your case the ListActivity) is on top - instead of creating a new instance, it would reuse the existing one. However, if you are only searching from MainActivity (hitting back on ListActivity after a search), then you are destroying the ListActivity instance and then creating a new instance - leading to onCreate() being called, but not onNewIntent().

这篇关于SearchView会无意启动可搜索的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆