简体   繁体   English

活动堆栈问题

[英]Activities Stack Issue

I have two sets of Activities suppose 3 Activities each set, (A1,B1,C1 || A2,B2,C2) I start my App from A1 then -> B1 -> C1 here I want to jump from C1 to -> A2 and at A2 if I press back it should exist the App and not put me back for C1, then from A2 I navigate to -> B2 -> C2. 我有两套活动假设每个活动3个活动,(A1,B1,C1 || A2,B2,C2)我从A1开始我的应用程序然后 - > B1 - > C1这里我要从C1跳转到 - > A2在A2,如果我按回它应该存在应用程序而不是让我回到C1,然后从A2我导航到 - > B2 - > C2。

So it is basically I want to change the starting Activity, it is like I have two Apps in one App and when I flip to the second App I have to clear the Activity Stack. 所以基本上我想改变起始活动,就像我在一个应用程序中有两个应用程序,当我转到第二个应用程序时,我必须清除活动堆栈。 Is that possible? 那可能吗? Any Ideas? 有任何想法吗?

Seems to me you've answered your own question. 在我看来,你已经回答了自己的问题。 You wrote: 你写了:

So it is basically I want to change the starting Activity, it is like I have two Apps in one App and when I flip to the second App I have to clear the Activity Stack. 所以基本上我想改变起始活动,就像我在一个应用程序中有两个应用程序,当我转到第二个应用程序时,我必须清除活动堆栈。

I would do it this way: 我会这样做:

Create a DispatcherActivity which is the activity that gets launched when the application is started. 创建DispatcherActivity ,它是在启动应用程序时启动的活动。 This Activity is the root activity of your task and is responsible for launching either A1 or A2 depending... and NOT call finish() on itself (ie: it will be covered by A1 or A2 but still be at the root of the activity stack). 此活动是您的任务的根活动,负责启动A1或A2,取决于...而不是自己调用finish() (即:它将被A1或A2覆盖,但仍然位于活动的根部堆)。

In A1 , trap the "back" key and tell the DispatcherActivity to quit like this: A1 ,捕获“后退”键并告诉DispatcherActivity退出如下:

@Override
public void onBackPressed() {
    Intent intent = new Intent(this, DispatcherActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addExtra("exit", "true");
    startActivity(intent);
}

This will clear the task stack down to the root activity ( DispatcherActivity ) and then start the DispatcherActivity again with this intent. 这将清除任务堆栈到根活动( DispatcherActivity ),然后再次使用此意图启动DispatcherActivity

In C1 , to launch A2 , do the following: C1 ,要启动A2 ,请执行以下操作:

Intent intent = new Intent(this, DispatcherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("A2", "true");
startActivity(intent);

This will clear the task stack down to the root activity ( DispatcherActivity ) and then start the DispatcherActivity again with this intent. 这将清除任务堆栈到根活动( DispatcherActivity ),然后再次使用此意图启动DispatcherActivity

In DispatcherActivity , in onCreate() you need to determine what to do based on the extras in the intent, like this: DispatcherActivity ,在onCreate()您需要根据intent中的extras确定要执行的操作,如下所示:

Intent intent = getIntent();
if (intent.hasExtra("exit")) {
    // User wants to exit
    finish();
} else if (intent.hasExtra("A2")) {
    // User wants to launch A2
    Intent a2Intent = new Intent(this, A2.class);
    startActivity(a2Intent);
} else {
    // Default behaviour is to launch A1
    Intent a1Intent = new Intent(this, A1.class);
    startActivity(a1Intent);
}

In A2 , trap the "back" key and tell the DispatcherActivity to quit using the same override of onBackPressed() as in A1 . A2 ,捕获“后退”键并告诉DispatcherActivity使用与A1相同的onBackPressed()覆盖来退出。

Note: I just typed this code in, so I've not compiled it and it may not be perfect. 注意:我只是输入了这段代码,所以我没有编译它,它可能不完美。 Your mileage may vary ;-) 你的旅费可能会改变 ;-)

if you want to close your app when press the back button instead of go back last activity, you should overwrite the back button. 如果要在按后退按钮而不是返回上一个活动时关闭应用程序,则应覆盖后退按钮。 in the overwrite method call finish() method after close the activity. 在overwrite方法中,在关闭活动后调用finish()方法。 i hope this may help you. 我希望这可以帮助你。

edited: 编辑:

check the below link : this is for close all activities in your view stack. 检查以下链接:这是用于关闭视图堆栈中的所有活动。 before closing your app close all activities. 关闭应用程序之前关闭所有活动。

http://www.coderzheaven.com/2011/08/27/how-to-close-all-activities-in-your-view-stack-in-android/ http://www.coderzheaven.com/2011/08/27/how-to-close-all-activities-in-your-view-stack-in-android/

You can check when the button that is pressed in Activity A2 and then if its a Back button you can close the app. 您可以检查在活动A2中按下的按钮的时间,然后检查其后退按钮是否可以关闭应用程序。 You can use the following method in A2 您可以在A2中使用以下方法

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

尝试使用此意图启动活动A2 - intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Handle this using onBackPressed and finish methods. 使用onBackPressed处理此方法并完成方法。

Before launching other activity, better you close the current activity using finish() method. 在启动其他活动之前,最好使用finish()方法关闭当前活动。

If you want to go to previous activity on back press, override onBackPressed method and call the particular intent. 如果要在后退时转到上一个活动,请覆盖onBackPressed方法并调用特定意图。

In A2 activity, add finish method in onBackPressed method (dont call previous activity here). 在A2活动中,在onBackPressed方法中添加finish方法(不要在此处调用以前的活动)。 This is one of the way. 这是其中一种方式。

while passing Intent from Activity C1 to A2 use as 将Intent从Activity C1传递给A2时使用as

Intent intent=new Intent(C1.this,A2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

and in Your Activity A2 Override Back Button by 并在您的活动A2中覆盖后退按钮

 @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        A2.this.finish();
    }
    return super.onKeyDown(keyCode, event);
}

FLAG_ACTIVITY_CLEAR_TOP is not what you are looking for. FLAG_ACTIVITY_CLEAR_TOP不是您要找的。 i think you are looking for: 我想你正在寻找:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM