How does FLAG_ACTIVITY_CLEAR_TASK works?

Viewed 5112

I'm very confused about FLAG_ACTIVITY_CLEAR_TASK in combination to FLAG_ACTIVITY_NEW_TASK. Isn't FLAG_ACTIVITY_CLEAR_TASK supposed to clear TARGET task but not caller task?

Let's assume I have this navigation

A -> B -> C 

Where B calls C with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK. How is that supposed to clear A and B from stack? I know that's happening because I tested it (If I click back on C, I leave the app), but I don't fully understand why... A and B are part of a different task that is not the task C will be created on.

FLAG_ACTIVITY_CLEAR_TASK

If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.

Maybe text in bold above means that caller and target task are cleared. It's unclear to me what associated would be.

2 Answers

FLAG_ACTIVITY_CLEAR_TASK will clear all existing task by starting the new activity in your case it's C.

I think you need FLAG_ACTIVITY_CLEAR_TOP and this will clear only the top activity from stack in your case B.

You might be interesting with other flags, check here.

FLAG_ACTIVITY_CLEAR_TASK can only be used with FLAG_ACTIVITY_NEW_TASK and it doesn't create a new task. All the activities in the current task are getting finished and the new activity(C in your case) becomes the root of the task.

FLAG_ACTIVITY_CLEAR_TOP with FLAG_ACTIVITY_NEW_TASK in your case will create a new task and make C the root of that task.

You can read more about how exactly FLAG_ACTIVITY_CLEAR_TASK and FLAG_ACTIVITY_CLEAR_TOP work here.

Related