本文介绍了完成特定活动的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序中可能有许多活动,最后启动的活动停留在堆栈顶部,然后按回去以完成当前活动.我有一个Activity序列,这是流程..

There can be many activities in application and the last launched activity stay on top of stack and on pressing back it finish the current activity.I have a sequence of Activity and here is the flow ..

如果我们有A,B, C(1),D, C(2) ...活动C(1)和C(2)是两个在浏览应用程序时启动了活动C的不同实例.因此,必须清除活动C的所有实例,结果应该是当我完成 C(2)之后,我应该离开这些堆栈 A,B,D .我该怎么办.

if we have A,B,C(1),D,C(2)... Activity C(1) and C(2) are two different instance of Activity C launched while navigating the application .So what is requisite is to clear all the instance of activity C and the result should be when I finish C(2) I should have left with these stack A,B,D. What should I do .

IMP -我想保持 C(1)在堆栈中的生命,直到我完成C(2)为止因为我可以使用New Task标志而不是创建这些实例来开始C,但是这些实例具有不同的UI和工作.

IMP -I want to keep the C(1) alive in stack until unless I finish the C(2)as I could have started C with New Task flag rather than creating these instances , but these instance have different UI and work .

以下方法不利.

第一

@Override
public void onBackPressed(){
    super.onBackPressed();
    Intent intent = new Intent(C(2).this , D.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    startActivity(intent);
}

这将清除堆栈中的所有活动并重新启动活动

This will clear All the activity from Stack and relaunch Activity

第二

在单例课程中跟踪活动",然后重新启动所需的流程,那么当有许多活动要开始时,这会浪费多少时间.

Keep the track of Activity in singleton class and then relaunching the required flow, how ever this will consume the time when there are many Activities to be started .

所以我认为应该有一些使用软件包管理器的解决方案或其他可以解决问题的解决方案,解决方案值得赞赏

So I thought that there should be some solution using package manager or other that will resolve the problem , solution are appreciated

推荐答案

我不知道在完成活动C2时手动关闭活动C1的方法.

I don't know a way to close activity C1 manually when finishing activity C2.

但是,您可以通过这种方式在活动C1上处理活动C1-

However you can take care of activity C1 in it's on resume this way -

1-在应用程序类中设置一个标志:

1 - Set a flag in your application class :

public static boolean IsClosingActivities = false;

可以在活动C2的完成"之前立即由C2将该值设置为"true".

This value can be set "true" by C2 right before "finish" of activity C2 occurs.

并在调用新活动C的startActivity的位置设置"false".(假设活动C的新实例可以在应用程序的后面创建).

And set "false" in the point where you'll call startActivity for a new activity C. (Assuming that new instances of activity C can be created later in the application).

2-在简历上的C实现:

2 - In C implementation of on resume :

    @Override
    protected void onResume() {
        super.onResume();
        if (YourApplication.IsClosingActivities) {
                this.finish();
        }
    }

这样-当用户从D导航回时-C1将完成操作,然后将其导航到B.

This way - when the user navigates back from D - C1 will finish itself and he will be navigated to B.

这篇关于完成特定活动的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 21:02