1

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?
Sowmia Sundararajan
  • 1,613
  • 2
  • 14
  • 17

1 Answers1

4

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.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Thanks David. is there any Intent Flag, by which i can achieve it? Since I have a custom class (Navigation Controller) which controls all the Navigation and clearing activities based on config.xml placed in the application. – Sowmia Sundararajan Feb 03 '14 at 14:01
  • 2
    Sorry no. There is no `Intent` flag that you can set that will clear the current task if you are launching an `Activity` into another task. For that you will need a "delegate" or "proxy" activity like I described. Please also note that `Intent.FLAG_ACTIVITY_CLEAR_TASK` is only available on Android 3.0 and higher, so your app will not run on Android 2.2/2.3 (still represents more than 20% of the installed base of devices) – David Wasser Feb 03 '14 at 14:15
  • I am gonna use support library for Intent.FLAG_ACTIVITY_CLEAR_TASK.
    Intent i = new Intent(this, Activity_C.class); ComponentName cn = i.getComponent(); Intent mainIntent = IntentCompat.makeRestartActivityTask(cn); startActivity(mainIntent);
    – Sowmia Sundararajan Feb 03 '14 at 14:39
  • Hmmm. Not sure that will work. Documentation indicates that this will relaunch the root activity (in your case `MainActivity`. Since your app has multiple tasks this may not do what you want. Also, I'm not sure that the behaviour of `FLAG_ACTIVITY_CLEAR_TASK` is fully/properly simulated in the compatibility library. Make sure you test all this. – David Wasser Feb 03 '14 at 14:47
  • 1
    Also, I hope you understand that if your app has multiple tasks it is often difficult/confusing for the user to go to the HOME screen and then return to your application using the list of recent tasks. Make sure that you test those conditions as well. – David Wasser Feb 03 '14 at 14:48
  • Is there any other way by which I can clear a set of activities while moving to other activity as described above ? without using taskAffinity (since creating multiple recent list for same app as you told) – Sowmia Sundararajan Feb 04 '14 at 12:01
  • Yes. In general you can set `FLAG_ACTIVITY_CLEAR_TOP` when launching an Activity. If this Activity is already in the stack then Android will finsh that Activity and all of the Activities on top of it in the stack, then launch a new instance of that Activity. If you also set an "extra" on the Intent that you use to start the Activity (for example "launchActivityX") then in `onCreate()` of the Activity that you just launched you can test for the existence of the "extra" and launch the new Activity accordingly. – David Wasser Feb 04 '14 at 18:49
  • Basically you delegate the launching of the target Activity to the one that you are clearing back to. I hope this is clear. – David Wasser Feb 04 '14 at 18:49