gpt4 book ai didi

Android:将搜索查询转发到一个处理搜索的 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:51:42 24 4
gpt4 key购买 nike

我有一个处理搜索的 Activity (ACTIVITY_1),当我在这个 Activity 中/从这个 Activity 中使用搜索(通过手机上的搜索按钮)时,它工作得很好。

但是,当我通过实现 onNewIntent 使用来自另一个 Activity (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在所有其他 Activity 中?

目前我必须输入 <intent-filter>在每个 Activity 中“激活”我的自定义搜索,然后将查询转发到通过 onNewIntent 处理搜索的 Activity (如上图)

<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>

最佳答案

我不确定我是否理解您描述的事件链,但在以下情况下您需要如何配置您的应用程序:ACTIVITY_1 是您始终希望在用户按下时从所有其他 Activity 启动的搜索 Activity “搜索”按钮。

假设搜索按钮在 Activity1 上完美运行,您只需向您的应用程序添加一些粘合元数据,告诉它您的所有其他 Activity 都应该使用 ACTIVITY_1 进行搜索,如下面的 list fragment 所示:

<application>
<meta-data
android:name="android.app.default_searchable"
android:value=".ACTIVITY_1" />

<!-- All your activities, service, etc. -->

</application>

使用它,您应该能够从除 ACTIVITY_1 之外的所有 Activity 中删除 Intent 过滤器,并且您不需要在任何其他 Activity 中使用 onNewIntent 处理程序。

关于Android:将搜索查询转发到一个处理搜索的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1906964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com