简体   繁体   English

Android 任务间切换

[英]Android switching between tasks

I'm trying to find a way to switch between tasks in android.我正在尝试找到一种在 android 中的任务之间切换的方法。

Let's say I have Activity A, B, C, D.假设我有活动 A、B、C、D。

Activity A and B are in one task(affinity)活动 A 和 B 在一个任务中(亲和力)

While C and D are in another task.而 C 和 D 正在执行另一项任务。

This is the manifest code of C activity which creates a new task.这是创建新任务的 C 活动的清单代码。

 <activity
        android:name=".CActivity"
        android:launchMode="singleTask"
        android:taskAffinity="com.new" />

A, B created in one task and C, D created in another task. A、B 在一个任务中创建,C、D 在另一个任务中创建。 Now, we need to just switch from D to B without creating a new instance of B. Also could able to switch from A to D without creating a new instance of D.现在,我们只需要从 D 切换到 B 而不创建 B 的新实例。也可以从 A 切换到 D 而不创建 D 的新实例。

TASK 1: A > B TASK 2: C > D任务 1:A > B 任务 2:C > D

Edit: These two stacks are from the same app.编辑:这两个堆栈来自同一个应用程序。 The reason I'm using two is one is my normal app and another stack is for the video call feature in my app.我使用两个的原因是一个是我的普通应用程序,另一个堆栈用于我的应用程序中的视频通话功能。 So that user can do video call & minimize the window and use the normal flow of the app.这样用户就可以进行视频通话并最小化窗口并使用应用程序的正常流程。

我认为你应该在活动 B 和活动 D 上设置标志FLAG_ACTIVITY_REORDER_TO_FRONT 。如果这些活动是以前创建的,它们将从活动堆栈中被带到前面。

I assume that you don't want to use Intent because whenever you use Intent for moving to activity A pressing Back key will move to the previous activity (activity C).我假设您不想使用 Intent,因为每当您使用 Intent 移动到活动时,按返回键将移动到上一个活动(活动 C)。 In this case I would suggest you to include FLAG_ACTIVITY_CLEAR_TOP flag.在这种情况下,我建议您包含 FLAG_ACTIVITY_CLEAR_TOP 标志。 It will destroy all the previous activity and let you to move to Activity A.它将销毁所有以前的活动,让您移动到活动 A。

Intent a = new Intent(this,A.class);
     a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(a);

Alternatively, you can try the FLAG_ACTIVITY_REORDER_TO_FRONT flag instead, which will move to activity A without clearing any activity.或者,您可以尝试使用 FLAG_ACTIVITY_REORDER_TO_FRONT 标志,它会在不清除任何活动的情况下移动到活动 A。

Or if you dont want to create new instance then或者,如果您不想创建新实例,则

Set launchMode of Activities to singleInstance in AndroidManifest.xml在 AndroidManifest.xml 中将活动的 launchMode 设置为 singleInstance

Hope this helps.希望这可以帮助。

or或者

1.) Make B a "SingleTask" activity. 1.) 使 B 成为“SingleTask”活动。 You can do this in the Android Manifest.您可以在 Android 清单中执行此操作。 To be brief, this means that only one 'instance' of B will ever exist within this Task.简而言之,这意味着在此任务中永远只存在 B 的一个“实例”。 If B is already running when it's called then it will simply be brought to the front.如果 B 在被调用时已经在运行,那么它只会被带到前面。 This is how you do that.这就是你这样做的方式。

<activity
        android:name=".ui.MyActivity"
        android:launchMode="singleTask"/>

But you don't want to just bring B to the front.但你不想只是把 B 带到前面。 You want to 'fall back' to B so that your stack looks like你想“回退”到 B,这样你的堆栈看起来像

A--> B A--> B

2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed. 2.) 将此标志添加到“开始”B 的意图中。这可确保删除 C 和 D。

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed.现在,当“D”向 B 调用新的 Intent 时,B 将被恢复,C 和 D 将被删除。 B will not be recreated, it will only call onNewIntent. B 不会被重新创建,它只会调用 onNewIntent。

OR或者

You start Activities Using StartActivityForResult()您使用 StartActivityForResult() 开始活动

And based on conditions You set ResultCodes in Activities.. Something like this..并根据您在活动中设置 ResultCodes 的条件.. 像这样..

GO_TO_ACT_A=1; GO_TO_ACT_A=1; GO_TO_ACT_B=2; GO_TO_ACT_B=2;

And in all Activies onActivityResultMethod check the result code..并在所有 Activies onActivityResultMethod 检查结果代码..

if(resultCode==GO_TO_ACT_A){
    finish(); //Assuming curently you are in Activity C and wanna go to Activity A
}

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

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