Clearing stack including activities in different tasks - android

I have a main activity A. There are two scenarios
1) A launches B.
B has launchmode singleTask and is launched with FLAG_ACTIVITY_NEW_TASK.
now I have a menu option in B which performs a delete operation and starts the activity A.
2) A launches B, which launces C it also contains the menu option to perform delet opereation.
I want A to be started with clearing the stack in both the scenarios but the activities belonging to another task still present there
I am stuck is there a way to clear the stack.

try using following code on delete opereation on both B and C activity
Intent intent=new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

If i talk about your second scenario then FLAG_ACTIVITY_TOP_CLEARwill clear the stack..
And your stack now will be only A instead of A-B-C-A.
and in second case AFAIU your problem you have only two activity A and B so if you want to restart A then after restarting A manually finish B.
Hope you got some trick.
Another thing if you have activity with launchmode=SingleTask then you can use flag_activity_brought_to_front.
That will act like
A-B-c to A

Why not finishing B and C before calling A again?
finish();

My method can not meet your goal A to be started with clearing the stack,But when user choose delet opereation and start B from A again the task that include A and B will be reset.
Use the flag FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET to mark the task will be clear when needed when you first start activity B from A, then if user choose delet opereation menu item from B or C, you set a flag, so next time from A(A should be single_task launch mode in manifest), you use the flag FLAG_ACTIVITY_RESET_TASK_IF_NEEDED to start B. This flag will clear B and all top of the B.
Hope this will help you :)

Related

The behavior after adding FLAG_ACTIVITY_NEW_DOCUMENT flag

I need to start an activity which will be added into the overview screen (recent task list).
I found this document and follow the guide:
https://developer.android.com/guide/components/activities/recents.html
In activity A, I add "FLAG_ACTIVITY_NEW_DOCUMENT" flag to my intent and start activity B with it.
I see both A and B in the list, but when I close activity A, I can still see B in the list. And when I go back to activity B, it seems to be destroyed and create again. (it looks like the activity has been restarted)
My question is:
How to automatically close B after I close A?
Is there any way to prevent the restarting process?
simply call finish(); after starting your Activity

Android: prevent activity to be instantiated more than once

I was going through Android Task and Back Stack documentation and at one point they mention this:
if your application allows users to start a particular activity from more than one activity, a new instance of that activity is created and pushed onto the stack (rather than bringing any previous instance of the activity to the top). As such, one activity in your application might be instantiated multiple times (even from different tasks), as shown in figure 3. As such, if the user navigates backward using the Back button, each instance of the activity is revealed in the order they were opened (each with their own UI state)
Let's take an example:
I have Activity A Starting Activity B which Starts Activity C which starts D.
Stack is A->B->C->D now it is possible to Start C from D so when we start C from D stack will be
A->B->C->D->C
Now instead of this standard behavior I want Activity to have only 1 instance or only 1 entry in the Back Stack. "SingleTop" will not work since Activity C was not on top when we started it from D.
I might be missing something but is there any way to achieve this making sure an activity has only 1 backstack entry?
Thanks
Pranay
Use Intent.FLAG_ACTIVITY_CLEAR_TOP, e.g.:
Intent intent = new Intent(context, <your_activity_here>);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);
So, all the activities in stack after activity C will be finished automatically. If you use the specified flag
A->B->C->D
will become
A->B->C
You can also use android:launchMode="singleInstance" in your activity tag in manifest

Get my Application Activity-Stack

I got 3 activities in my app. Activity A,B and C.
I am in activity B now and I need to know if there are more activities in my activity stack or when I finish activity B I will exit the app.
I found ActivityManager that can do the job, how ever it require GET_TASKS permision.
Is there any other way for doing it?
While going from one activity to another activity you can set flags clear top to delete other activity those are now on stack and then go to new activity. If you do this then finish the current activity then you are out of the app.
For example
Intent imra = new Intent(getApplicationContext(),
MoreActivity.class);
imra.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(imra);
finish();
Unfortunately the answer is just no!

Go back to a specific activity

Here's what I want to do:
If I use startActivity I start a new Activity and I can't use StartActivityForResult from Main Menu -> Total. I would like to go back on the Main Menu activity already present in my stack and remove Activities A, B ,C and Total.
Have you tried using FLAG_ACTIVITY_CLEAR_TOP flag in your intent (used to start "Main Menu" from "Total")?
http://developer.android.com/reference/android/content/Intent.html#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.
To remove activities A,B,C, and Total from your stack, just call finish() on these 4 activites.
See also : Tasks & Back stacks and Activity task Design

How do I clear all Activities from the stack?

I am having trouble popping all activities off the stack using Intent.FLAG_ACTIVITY_CLEAR_TOP and android:launchMode="singleInstance".
In my application activity A, launches activity B (via startActivity) which in turn launches activity C (via startActivity). On activity C the user presses a menu item to return to activity A. When they arrive at activity A, I want only A on the stack such that if they click the back button they return to the home screen (desktop).
This is the code that I am currently using when the user presses a button to return to A:
Intent i = new Intent(this, A.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
All activities are defined as android:launchMode="singleInstance" in the project manifest.
My code doesn't seem to work though. Once I'm back on activity A if I click the back button I return to activity C. Am I misunderstanding how to use Intent.FLAG_ACTIVITY_CLEAR_TOP?
I've always found the best way to ensure C would be removed from the stack is to call finish() after startActivity to remove C from the stack.
The documentation does read as though things would behave the way you expected them to, but it would seem this isn't happening, so finish() will ensure C is removed.
I usually use the technique Al suggested (calling finish() after starting the new activity).
You could also experiment with task affinity. I've never done that myself, but it may be relevant in your case as well. See this thread: http://groups.google.com/group/android-developers/browse_frm/thread/ca3b26a14d024597/129e37375105901b

Categories

Resources