FLAG_ACTIVITY_NEW_TASK not working as expected - android

I have a stack with 3 Activities: A, B, C. From Activity C I start the Activity B setting on the Intent the flag: FLAG_ACTIVITY_NEW_TASK. According to the documentation this should bring the Activity B with the underlying task on the front resulting in a stack : A, B, C, A, B.
However the actual stack is A, B, C, B. When I press the Back-Button from the Activity B I go through the above stack in the reverse order: B -> C -> B -> A.
The code which starts the the Activity B from Activity C is:
Intent intent = new Intent(this, B.class);
intent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
According to the docs the flag FLAG_ACTIVITY_NEW_TASK causes the same behavour as the "singleTask" launchMode value. If I set in the AndroidManifest.xml file the launchmode "singletask" on the Activity B, then it produces the expected behavour as described in the docs: the task A, B comes to the front resulting in a stack A, B, C, A, B.
Why does not it work with the flag FLAG_ACTIVITY_NEW_TASK? Is some affinity configuaration also necessary in this case?

Related

how to destroy last two previous visited activity in android

For example I have four activity A, B, C, and D. I go from A to B and from B to C and then from C to D.
Now in Activity D, on particular button click, I want to destroy Activity D as well as activity C and want to jump on activity B. Now when I click on back button in activity B, then it should navigate me on activity A. please help someone how can I achieve this functionality.
Note: I do not want to destroy activity C while navigating from activity C to activity D
In D, when you want to navigate back to B, do this:
Intent intent = new Intent(this. ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
This will cause A and D to be removed from the stack, B will now be shown in whatever state it was in when you left it, and onNewIntent() will be called on B.

Android lauchmode and single instance

I don't understand how to do my use case between the launchmode of activity and the flag of intent.
What i want to do :
A => B => C => B when i back B => C => A
In other word, i want to have a single instance of all activity in the stack and if i recall one of the single instance the activity go to top of stack (with reset or destroy + recreate don't have importance, id o my logic of creation in onresume so), exception for my custom splashscreen (but i resolve this one with noHistory for this activity "splashscreen").
I tried standard mode with flag
Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP or with this flag Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but that don't have the behavior that i want.
So if you can tell me which launchmode and/or flag i should use for made what i want, i would be grateful.
FLAG_ACTIVITY_REORDER_TO_FRONT seems appropriate in your case:
FLAG_ACTIVITY_REORDER_TO_FRONT
If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then B will be brought to the front of the history stack, with this resulting order: A, C, D, B. This flag will be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.
If we leave out Activity D in the above description we get:
A, B, C (start B) => A, C, B
Which appears to be what you want.

Android back stack - Go back to a certain activity in back stack

I have activities A -> B -> C -> D. How can I open the existing B from activity D clearing C and D? I would end up with A -> B. I don't want to recreate a new B.
I think you must use FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP.
According to the doc:
consider a task consisting of the activities: A, B, C, D. If D calls
startActivity() with an Intent that resolves to the component of
activity B, then C and D will be finished and B receive the given
Intent, resulting in the stack now being: A, B.
The currently running instance of activity B in the above example will
either receive the new intent you are starting here in its
onNewIntent() method, or be itself finished and restarted with the new
intent. If it has declared its launch mode to be "multiple" (the
default) and you have not set FLAG_ACTIVITY_SINGLE_TOP in the same
intent, then it will be finished and re-created; for all other launch
modes or if FLAG_ACTIVITY_SINGLE_TOP is set then this Intent will be
delivered to the current instance's onNewIntent().
You can declare you target activity's(Activity B in your situation) Mode of SingleTop in Manifest file like
<activity android:name="YourActivityName" android:launchMode="singleTop"></activity>
Then startActivity for navigating to this Activity will end C and D and wont start a new Activity B
Right after you finish dealing with C and D, you can just finish() the activities.

How to pop out some activities

I need to do the following: I have 5 activities and I call them like this A->B->C->D->E but when activity E finishes, then it should return to activity B keeping A first, like this: A->B. How can I do that? Thanks
You want FLAG_ACTIVITY_CLEAR_TOP:
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
For example, consider a task consisting of the activities: A, B, C, D. If D calls startActivity() with an Intent that resolves to the component of activity B, then C and D will be finished and B receive the given Intent, resulting in the stack now being: A, B.
Don't create a new task.

calling an activity from stack instead of launching new instance

how can i call an activity from stack instead of launching new instance ?
here is a scenario :
calling activity A with parameters in order to retrieve data
navigate from A to B
navigate from B to C
i want launch A again but not with a new instance , i want get it from the stack so no need to pass parameters and wait to retrieve data.
If I get your point correctly you can simple exit your activity B and C with finish();.
So if you ActivityC finishes and also ActivityB the ActivityA should come to the front, which should be that what you want.
Try to use FLAG_ACTIVITY_REORDER_TO_FRONT. For example:
Intent intent = new Intent(BActivity.this, AActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
From javadocs:
public static final int FLAG_ACTIVITY_REORDER_TO_FRONT
Added in API
level 3
If set in an Intent passed to Context.startActivity(), this flag will
cause the launched activity to be brought to the front of its task's
history stack if it is already running.
For example, consider a task consisting of four activities: A, B, C,
D. If D calls startActivity() with an Intent that resolves to the
component of activity B, then B will be brought to the front of the
history stack, with this resulting order: A, C, D, B. This flag will
be ignored if FLAG_ACTIVITY_CLEAR_TOP is also specified.

Categories

Resources