什么是正确的方式来清除后台活动/ activites从栈?后台、正确、方式、activites

2023-09-12 05:07:43 作者:温柔尝尽了吗

作为问题标题所说 - 我需要知道什么是删除/销毁/完成是在栈中,目前暂停模式(不是特定情况下的某个地方活动的最佳方式 - 但具体的派生类)。

as the questions title says - I need to know what is the best way to "remove"/destroy/finish an activity that are somewhere in the middle of stack and currently on pause mode (not specific instances - but specific derived classes).

例如:     如果栈的当前状态是这样的:

for example: if the current state of the stack looks like this:

ActivityD   <-- top of the stack, currently forground
ActivityC
ActivityA
ActivityC
ActivityA

请求以清的所有ActivityC情况下,会导致栈是这样的:

a request to "clear" all ActivityC instances would cause the stack to be like:

ActivityD  <-- still top of the stack, currently forground.
ActivityA
ActivityA

我并不想这样做,取决于活动的启动模式或意图的标志。我知道如何使用他们和他们的利益。

I don't want to do that depends on activity launch mode or intent flags. I know how to use them and their benefits.

我目前知道我可以做的就是发送了需要的所有活动被破坏广播时,接收广播会听,并调用Activity.finish()。 这是工作,但它需要时,他们举办的活动已暂停接收器进行登记就算了,我不知道完成()方法被调用的暂停活动是正确的事情做。

what I currently know I can do is to send broadcast which all activities needed to be destroyed would listen to, and call Activity.finish() when receive the broadcast. that's working, but it requires receivers to be registered even when their hosting activity is paused, and I'm not sure "finish()" method been called from paused activity is something right to do.

那么,是否可以从恢复活动调用Activity.finish()方法?

is it right to call Activity.finish() method from resumed activity?

是不是注册接收的int的OnCreate()方法,并注销了他的OnDestroy()?

is it right to register receiver int the OnCreate() method, and unregister him OnDestroy()?

是不是从恢复活动的处理播出,并从该点调用finish()?

is it right to handle broadcast from resumed activity, and call finish() from that point?

有一个Android的方式或某些API,我不知道从堆栈中明确活动?

is there an "Android way" or some API I don't know about to clear activities from stack?

在此先感谢

推荐答案

请自定义广播接收器,并在每它可以在您选择的情况下被解雇的活动注册。在每一个活动onReceiveMethod(可选择)只是调用finish()。在此你的活动将会从堆栈中删除。此外,您可以访问这个更多的帮助: On注销,明确活动历史堆栈,preventing&QUOT;返回]无法打开登录的,只有Activites

Make a custom Broadcast receiver and register it in every activity which can be fired on event of your choice. in onReceiveMethod of every activity (may be selected )just call finish(). In this your activities will be removed from the stack. Further you can visit this for more help: On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites