【发布时间】:2009-12-15 11:59:37
【问题描述】:
我有一个活动处理搜索 (ACTIVITY_1),当我在此活动中/从该活动中使用搜索(通过手机上的“搜索”按钮)时,它可以完美运行。
但是,当我通过实现 onNewIntent 使用来自另一个活动 (ACTIVITY_2..x) 的搜索并将查询字符串转发到我的 Search_Activity.class (ACTIVITY_1)
@Override
protected void onNewIntent(Intent intent) {
Log.i(TAG, "onNewIntent()");
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
Log.i(TAG, "===== Intent: ACTION_SEARCH =====");
Intent myIntent = new Intent(getBaseContext(), Search_Activity.class);
myIntent.setAction(Intent.ACTION_SEARCH);
myIntent.putExtra(SearchManager.QUERY, intent.getStringExtra(SearchManager.QUERY));
startActivity(myIntent);
}
}
它总是先暂停ACTIVITY_2,然后转到ACTIVITY_2的onCreate()。
- 为什么它在我的 ACTIVITY_2 已经存在并且不直接转到 onNewIntent 时重新创建它?
- 还有其他方法可以将搜索查询直接转发到 ACTIVITY_1?例如通过 Manifest.xml 中的设置
- 是否可以将所有搜索查询自动转发到ACTIVITY_1,甚至无需在所有其他活动中实现
onNewIntent?
目前我必须在每个活动中添加一个<intent-filter> 以“激活”我的自定义搜索并将查询转发到通过onNewIntent 处理搜索的活动(如上所示)。
<activity android:name=".Another_Activity"
android:theme="@style/MyTheme">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
【问题讨论】:
标签: android search android-activity