سؤال

I am having three modules - ModuleA, ModuleB, ModuleC

ModuleA - 1 activity

MainActivity - no task affinity

Module B - 3 activities 

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

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

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

Module C - 1 activity 

Activity_D - no task affinity

Navigation Case 1:
  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

    Expected Task Result:
    Task com.performance.poc

    Activity_D
    MainActivity

    Task com.performance.poc.main
    Activity_C
    Activity_B
    Activity_A

    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. 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 Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK
  5. on btn Click - start Activity_D Intent.FLAG_ACTIVITY_NEW_TASK

Expected Task Result:

Task com.performance.poc
Activity_D
MainActivity

Task com.performance.poc.main

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.

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.

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?
هل كانت مفيدة؟

المحلول

Setting Intent.FLAG_ACTIVITY_CLEAR_TASK will clear the target task.

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

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

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top