活动堆栈问题 [英] Activities Stack Issue

查看:106
本文介绍了活动堆栈问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两套活动料想3活动各一套,(A1,B1,C1 || A2,B2,C2),我开始了我的应用程序从A1然后 - > B1 - > C1在这里,我想从C1到跳 - > A2和A2如果我preSS回到它应该存在于应用程序,而不是把我带回了C1,然后从A2我浏览到 - > B2 - > C2。

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.

所以基本上我想改变出发的活动,这就像我在一个应用程序两个应用程序,当我翻到第二个应用程序我要明确活动堆栈。那可能吗?任何想法?

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.

我会做这种方式:

创建一个 DispatcherActivity 这是当应用程序启动时被推出的活动。这个活动是你的任务的根系活力,负责启动A1或A2因...而不是调用完成()本身(即:它会被覆盖由A1或A2,但仍然是在活动堆栈的根目录)。

Create a DispatcherActivity which is the activity that gets launched when the application is started. 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 ,陷阱返回键,并告诉DispatcherActivity退出这样的:

In A1, trap the "back" key and tell the DispatcherActivity to quit like this:

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

这将清除任务堆栈下到根系活力( DispatcherActivity ),然后启动 DispatcherActivity 再次这种意图。

This will clear the task stack down to the root activity (DispatcherActivity) and then start the DispatcherActivity again with this intent.

C1 ,启动 A2 ,请执行以下操作:

In C1, to launch A2, do the following:

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

这将清除任务堆栈下到根系活力( DispatcherActivity ),然后启动 DispatcherActivity 再次这种意图。

This will clear the task stack down to the root activity (DispatcherActivity) and then start the DispatcherActivity again with this intent.

DispatcherActivity 的onCreate(),你需要决定如何根据该意向的群众演员,像这样的:

In DispatcherActivity, in onCreate() you need to determine what to do based on the extras in the intent, like this:

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);
}

A2 ,陷阱返回键,告诉DispatcherActivity使用 onBack pressed相同倍率()退出 A1

In A2, trap the "back" key and tell the DispatcherActivity to quit using the same override of onBackPressed() as in A1.

注:我刚才输入这个code的,所以我没有编译它,它可能不是完美的。你的情况可能会有所不同; - )

Note: I just typed this code in, so I've not compiled it and it may not be perfect. Your mileage may vary ;-)

这篇关于活动堆栈问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆