当搜索开始新活动时如何结束旧活动
我有一个应用程序,它在 ListView
中呈现某种项目目录。该列表相当长,因此我实现了 Search
功能,如下所示:
<activity android:name=".ItemsOverview"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- enable the search dialog to send searches to ItemsSearch -->
<meta-data android:name="android.app.default_searchable"
android:value=".ItemsSearch" />
</activity>
...
...
<activity android:name=".ItemsSearch">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
ItemsSearch
然后显示相同的 ListView
,但仅包含以下项目匹配搜索条件。
这有两个基本问题:
ItemsSearch
几乎是ItemsOverview
的重复(但对搜索功能进行了一些增强);ItemsSearch
覆盖ItemsOverview
,这样如果完成三到四次搜索,则需要按四到五次“后退”按钮才能退出。不太达到想要的效果。 :)
我想以某种方式在第二个活动开始时结束原始活动,并且理想情况下将两个类(概述和搜索)合并为一个。有没有办法让我的活动检测到它已经在另一个进程中运行,并在开始时终止该其他进程?
一旦我理解了这一点,我可能就能弄清楚如何将两者结合起来。当其他人需要使用过滤列表时该怎么办?
I have an application that presents a sort of catalog of items in a ListView
. The list is rather long, so I've implemented the Search
capability like this:
<activity android:name=".ItemsOverview"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- enable the search dialog to send searches to ItemsSearch -->
<meta-data android:name="android.app.default_searchable"
android:value=".ItemsSearch" />
</activity>
...
...
<activity android:name=".ItemsSearch">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
ItemsSearch
then presents the same ListView
, but containing only items that match the Search criteria.
There are two fundamental problems with this:
ItemsSearch
is an almost duplicate ofItemsOverview
(but with some enhancements for the search capability);ItemsSearch
overlaysItemsOverview
such that if three or four searches are done, it takes four or five presses of the Back button to get out. Not quite the desired effect. :)
I would like to, in some fashion, end the original Activity when the second Activity starts and, ideally, combine the two Classes (Overview and Search) into one. Is there a way for my Activity to detect that it's already running in another process, and to kill that other process at inception?
Once I understand this, I can probably figure out how to combine the two. What do others do when they need to utilize a filtered list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解的话,您希望清除活动返回堆栈。调用搜索活动时使用活动标志。
这是有关活动返回堆栈的开发人员页面。
If I understand, you would you like to clear the Activity Back Stack. Use the activity flags when you call your search Activity.
This is the developer page about the activity back stack.
要在搜索启动新活动时结束旧活动,请在 odler 活动中给出 finish() 并调用新活动。
Intent意图 = new (Intent(MainActivity.this,GPSActivity.class);
startActivityForResult(意图, ACTIVITY_GPS);
finish();
在这里我们可以启动新的 Activity GPSActivity 并完成 MainActivity。
To end old Activity when Search starts new Activity give finish() in odler activity and call new activity.
Intent intent = new (Intent(MainActivity.this,GPSActivity.class);
startActivityForResult(intent, ACTIVITY_GPS);
finish();
In this we can start new Activity GPSActivity and finishes MainActivity.