Android:任务关联性&清除任务 [英] Android: Task affinity & Clear task

查看:66
本文介绍了Android:任务关联性&清除任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个模块-ModuleA,ModuleB,ModuleC

I am having three modules - ModuleA, ModuleB, ModuleC

ModuleA - 1 activity

MainActivity-无任务关联

MainActivity - no task affinity

Module B - 3 activities 

Activity_A 任务相似性="com.performance.poc.main"

Activity_A task affinity = "com.performance.poc.main"

Activity_B 任务相似性="com.performance.poc.main"

Activity_B task affinity = "com.performance.poc.main"

Activity_C 任务相似性="com.performance.poc.main"

Activity_C task affinity = "com.performance.poc.main"

Module C - 1 activity 

Activity_D-无任务关联

Activity_D - no task affinity

Navigation Case 1:

  1. MainActivity
  2. 在btn上单击-启动Activity_AIntent.FLAG_ACTIVITY_NEW_TASK
  3. 在btn上单击-启动Activity_B
  4. 在btn上单击-启动Activity_C
  5. btn上的
  6. 单击-启动Activity_D Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK

  1. MainActivity
  2. on btn Click - start Activity_A Intent.FLAG_ACTIVITY_NEW_TASK
  3. on btn Click - start Activity_B
  4. on btn Click - start Activity_C
  5. on btn Click - start Activity_D Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK

预期的任务结果:
任务com.performance.poc

Activity_D
MainActivity

Expected Task Result:
Task com.performance.poc

Activity_D
MainActivity

任务com.performance.poc.main
Activity_C
Activity_B
Activity_A

Task com.performance.poc.main
Activity_C
Activity_B
Activity_A

预期:启动Activity_D任务com.performance.poc.main时应清除. 实际:仍保留Activity_A,Activity_B,Activity_C,但MainActivity已清除.

Expected : on starting the Activity_D task com.performance.poc.main should be cleared. Actual : Still Activity_A, Activity_B, Activity_C remains but MainActivity is cleared.


Navigation Case 2:

  1. MainActivity
  2. 在btn上单击-启动Activity_AIntent.FLAG_ACTIVITY_NEW_TASK
  3. 在btn上单击-启动Activity_B
  4. 在btn上单击-启动Activity_C Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
  5. 在btn上单击-启动Activity_DIntent.FLAG_ACTIVITY_NEW_TASK

预期任务结果:

Expected Task Result:

任务com.performance.poc
Activity_D
MainActivity

Task com.performance.poc
Activity_D
MainActivity

任务com.performance.poc.main

Task com.performance.poc.main

Activity_C
Activity_B
Activity_A

Activity_C
Activity_B
Activity_A

 Expected : on starting the Activity_C, in task com.performance.poc.main, Activity_A, Activity_B should be cleared. 
Actual :  Activity_A, Activity_B is cleared as expected.

我的问题是,为什么在Case1中,即使Activity_A,Activity_B,Activity_C处于同一任务中,也无法清除它们并清除MainActivity.

My Question here is why in Case1, even though Activity_A, Activity_B, Activity_C are in same task and it is not clearing these and clearing MainActivity.

Clear_Task应该清除活动的现有任务,并以new_task和clear_task的意图从中调用startActivity.否则它将清除目标活动的任务.

The Clear_Task should clear the existing task of the activity from which startActivity is called with intent new_task and clear_task. or it will clear the task of target activity.

If it is Target activity, I need to clear the task of the leaving activity, is there any way to do it?

推荐答案

设置Intent.FLAG_ACTIVITY_CLEAR_TASK将清除目标任务.

您说您需要清除当前任务.您可以通过使用中间Activity来做到这一点.只需创建一个简单的Activity即可在onCreate()中执行以下操作:

You say that you need to clear the current task. You can do this by using an intermediate Activity. Just create a simple Activity that does the following in onCreate():

Intent = new Intent(this, ActivityD.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

这个简单的活动应具有与ActivityA, B and C相同的taskAffinity.

This simple Activity should have the same taskAffinity as ActivityA, B and C.

ActivityC要启动ActivityD时,它应该像这样启动该活动:

When ActivityC wants to start ActivityD, it should start this activity instead like this:

Intent = new Intent(this, SimpleActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);

这将清除当前任务,然后SimpleActivity将启动ActivityD并完成,这将完成任务.

This will clear the current task and then SimpleActivity will launch ActivityD and finish, which will finish the task.

这篇关于Android:任务关联性&清除任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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