SearchView会无意启动可搜索的活动 [英] SearchView launches searchable activity without intent
问题描述
我正在使用SearchView来启动一个显示搜索结果的新活动.我遵循以下来源:
I am using a SearchView for starting a new activity that shows the search results. I have followed the following sources:
- Android教程:创建搜索界面
- SO:从SearchView启动新活动
- SO:无法在操作栏中获得搜索视图才能正常工作
- 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)
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屋!