Flag_activity_new_task vs flag_activity_clear_top năm 2024

Looks like every tutorial that we have got out there on the web mentions that FLAG_ACTIVITY_NEW_TASK starts a new task if the activity we are starting is not currently running in the task.But it seems that using FLAG_ACTIVITY_NEW_TASK doesn’t Creates a new task always, it is only creating a new task if there is no task available for the activity we need to run like when we start an activity from a BroadCastReceiver using the context inside onReceive() .

According to the all the tutorials on the web,

Suppose we have got following activities in our backStack-

A->B->C

Now If We want to start a new Activity D, then it should start in a New Task, but this doesn’t seems to happens and the activity D is Started in the Same task and we have out final backstack

A->B->C->D

AnyOne Who Can Clarify?

I know setting taskAffinity will force to create a new task but this brings another problem:

Try Yourself:

Suppose we launch two Activity:

A-B

Then again we launch one other Activity with android:taskAffinity**,So now we have got**

A-B | C
A-B (on the same task)
C (on other task)

Now Again We start

A->B->C->D

0 ,So now our BackStack will Look like:

A-B|C-A-B
A-B(on the same task)
C-A-B(on other task)

Now, the problem is If We will Again try to start C from B (from task C-A-B), nothing happens and C will not get launch (I din’t know why?) Here’s a gif showing the problem…

Flag_activity_new_task vs flag_activity_clear_top năm 2024


At August 30, 2018, 9:31pm, mmurphy replied:

Without code, it is difficult to comment on what your code does.

I know setting taskAffinity will force to create a new task

No, it just sets the task affinity. Whether a task is created depends on whether that task already exists or not at the time you start the activity.

Now, the problem is If We will Again try to start C from B (from task C-A-B), nothing happens and C will not get launch (I din’t know why?)

Again, without code, it is difficult to comment.

Tasks are one of the most arcane areas of Android UI development, so I’m not surprised that you are encountering problems. Personally, outside of very specific scenarios, I try to avoid task management entirely.

I am sorry that I cannot provide much help as it stands. If you want help with what your code is doing, I would need to be able to see that code.


At August 31, 2018, 4:03am, hackzcorporation replied:

@mmurphy

Tasks are one of the most arcane areas of Android UI development, so I’m not surprised that you are encountering problems. Personally, outside of very specific scenarios, I try to avoid task management entirely.

I agree with you, it is one of the most confusing thing i have ever encountered with.

For past 4 days i’m just bashing my head into the walls, every time it looks like now i understand how tasks are working one more problem arises and i remain stuck there.Please look at the source code and try to help me with task management. https://we.tl/t-gG0qiZMn79


At August 31, 2018, 11:55am, mmurphy replied:

Your code seems focused on the second scenario from your original question. I am not completely certain what your overall business rules are for what should be going on with your activities outside of these experiments. So, here are two patterns that work:

  • If you want C to always be in a new task, remove its

    A->B->C->D

    1 and start it with

    A->B->C->D

    2. B will always launch C in a new task, though this will add new tasks for each such launch (since the decision of whether to launch C in a new task is hard-coded in your app).
  • If you want C, and activities launched by C, to be in a separate task, use

    A->B->C->D

    1, but then only start C in a new task the first time. Second and subsequent times, either use

    A->B->C->D

    4 to bring the existing C instance to the foreground, or use no flags to create a new C instance on this other task. This will require you to track some state, to know whether C should be started with FLAG_ACTIVITY_NEW_TASK or not.

                Intent intent= new Intent(SecondActivity.this,ThirdActivity.class);
                if (launchedAlready) {
                    // use intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); if you want to reuse the existing C instance
                }
                else {
                    launchedAlready = true;
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                }
                startActivity(intent);

For finer-grained control over the sequence of events, have only one activity, and use fragments to represent the different screens. This is Google’s long-term direction with Jetpack, as seen with things like the Navigation component.

What's the difference between flag_activity_clear_task and flag_activity_clear_top?

One obvious difference between this and FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP is that FLAG_ACTIVITY_CLEAR_TASK needs FLAG_ACTIVITY_NEW_TASK . But, other than that, it would seem like the net effects are the same, and also match FLAG_ACTIVITY_RESET_TASK_IF_NEEDED .

What is flag_activity_clear_top?

FLAG_ACTIVITY_CLEAR_TOP : This flag is used to clear all activities on top of the target activity, essentially removing any activities between the current activity and the target activity from the stack.

What does Flag_activity_new_task do?

When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag. A new activity, by default, is launched into the task of the activity that called startActivity() .

What is the difference between intent and activity in Android?

An activity is started or activated with an intent. An Intent is an asynchronous message that you can use in your activity to request an action from another activity, or from some other app component. You use an intent to start one activity from another activity, and to pass data between activities.