How to clear the complete back stack (and possible solution) - android

I have three activities A, B and C.
A is the main activity of my application.
A and C can also be started from the Options Menu, B is started from A.
I would like the following behavior:
application starts with A: back stack is {A}
from A, I navigate to B : back stack is {A,B}
from the options menu, I start C : back stack is {C}
1 and 2 are trivial but I don't succeed in getting 3 to work.
I tried quite a lot of FLAG_ACTIVITY combinations but without success
and I'm getting the impression that this isn't possible.
I'm thinking about creating a DummyRoot activity that is just used to start another activity (actual activity name to start is passed in Intent.getExtras()). I can give this DummyRoot activity the FLAG_ACTIVITY_CLEAR_TOP.
By doing so, I would get
application starts with DummyRoot(A): back stack is {DummyRoot, A}
from A, I navigate to B : back stack is {DummyRoot, A,B}
from the options menu, I start DummyRoot(C) : back stack is {DummyRoot, C}
Do you foresee problems with this approach?
Is this needed in the first place or is it possible to clear the back stack in a more elegant way?

So when you press back whilst in C, you want the application to quit? If so, you will need to use Flags in your manifest and when you start the Activity using the Intent. A combination here will allow you to clear the current Task and then start a fresh one with your new Activity in it. It does not seem a valid UX, but should do the trick. Please read up on the FLAGS in the documentation for more info on what they will actually do.

Related

Keep one activity in the bottom of the backstack

I want to learn the proper way to manage the activity back stack with regards to my issue. Most of the time when a person uses my app, I want to keep an activity in the bottom of the stack, let's call this Activity A. This would be their "Home" activity. I have a navigation view which can take the user to a bunch of other activities, but I want to manage what displays when they tap back. I want Activity A to always be the last activity in the stack, so the stack can look like A -> B -> C-> D, and when the user is on Activity D and they want to go to Activity E, I want the stack to look like A -> E when they press it.
A possible solution I have found is by clearing all the activities in the current stack, launching Activity A, and sending an intent for Activity E in the intent I launch Activity A with, then that will just check it's intent extras and if it finds an intent in the extras it would just launch that intent. This results in the stack looking the way I want, Activity A -> Activity E. I just want to know if there is a better or simpler way.
I have tinkered with the activity properties in my manifest, but it seems like I can't do exactly what I would like to with those.
Any help would be appreciated :)
Lets assume you want to keep activity A in the back stack so that whenever a user presses back, you want to show activity A on top. Lets say you go to B from A and then C from B. So whenever you go from any activity(other than A) to any other activity just call finish() from the calling activity, this will remove the stack entry of the corresponding activity, ensuring that only activity is there in the back stack.

Manipulating back task stack in android

I have three activities A,B,C,D.
When I use activity B, then previous activity is A. I will write it as A->B.
So when I click back I go to A.
However I want to start activity D from B. But instead of tree A->B->D I need to alter it to A->C->D. How can do it in android?
There are many ways to do it.
You can have a lok at the below links.
First- Second- and this-

What are the implications of using FLAG_ACTIVITY_SINGLE_TOP?

Could someone please explain what FLAG_ACTIVITY_SINGLE_TOP do? The docs say
If set, the activity will not be launched if it is already running at
the top of the history stack.
But that statement seems to be burying a great deal of meaning beneath it. For instance someone online mentioned that the top activity may not be the same as the activity at the top of the task stack. I have no idea what all of that means. Hence my greater question: what are the implications of using FLAG_ACTIVITY_SINGLE_TOP?
It means if the activity is already up and you call it again you wont replace it with a new one and you wont create another one (which sometimes happens and is evident when you hit back and you see the same activity up), instead you will pull that one up.
So say you have 3 activities: A -> b -> c. You are in C and you came to it through A and then B. If you call to A, from C with the SINGLE_TOP filter your stack will look like A, C, B - if you started to hit the back button, you would go to C, then B. I could be wrong but thats how i believe it is. You can also pass in the CLEAR filter with it to erase the back stack and techniquely be started back at A with no stack in back of it, you would back straight out to home. - please correct if im inaccurate.
The following is an answer I read online. It is not a complete answer to the question you are asking, so I am really hoping someone else can add a lot more meat to it.
If an instance of the activity already exists at the top of the
current task, the system routes the intent to that instance through a
call to its onNewIntent() method, rather than creating a new instance
of the activity. The activity can be instantiated multiple times, each
instance can belong to different tasks, and one task can have multiple
instances (but only if the the activity at the top of the back stack
is not an existing instance of the activity).
For example, suppose a task's back stack consists of root activity A
with activities B, C, and D on top (the stack is A-B-C-D; D is on
top). An intent arrives for an activity of type D. If D has the
default "standard" launch mode, a new instance of the class is
launched and the stack becomes A-B-C-D-D. However, if D's launch mode
is "singleTop", the existing instance of D is deliverd the intent
through onNewIntent(), because it's at the top of the stack: the
stack remains A-B-C-D. However, if an intent arrives for an activity
of type B, then a new instance of B is added to the stack, even if its
launch mode is "singleTop".
Source

How does android:noHistory="true" work?

Lets say I have a base activity with a menu, when I click on menu item A, it goes to activity A. I open the menu again, and go to B. From B I go back to A, and back and fourth like this for a while.
So the stack would be A, B, A, B, A, B, ....
And when I hit the back button, it goes backwards through the stack as expected.
However lets say I don't want this functionality, so I add to my manifest, android:noHistory="true". So when I hit the back button it exits the application instead of going though the stack.
Now the illusion makes it seem, lets say if I'm in activity A, I use the menu and go to activity B, the stack would just be B, because I can't go back to A.
But, when using noHistory="true", does the true stack of A, B, A, B, A, B exist? Rather, is every call to an activity by using the menu instantiating a new copy of that activity, but the user can't see it? Would this be causing resource issues?
Or when noHistory="false", does the back button just call something like startAcitvity(intent) again or is it going through each new copy that was instantiated?
I'm concerned with resource issues and not slowing down a users android device.
From the docs about noHistory:
A value of "true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.
Regarding your question:
does the true stack of A, B, A, B, A, B exist?
The docs would indicate no.
I'm concerned with resource issues and not slowing down a users android device.
You really don't need to worry about this. The OS should handle the cleanup of activities when memory is getting low. Its more likely that poor use of bitmaps or logic in your activities will result in performance slowdowns.
android:noHistory=“true” works :-
Let suppose you have opened "your app".
You are on homepage Activity now,
After it you go to the another(second) activity.Here from second activity you press the home button of mobile device or open the some other application.
Now again if you open "your app" it will go to the homepage of app instead of going to the activity which one you left the app(i.e.second activity).
I had few fragments in my app and it seemed difficult to get out to the home screen by pressing back button without entering Launcher Activity of my app. I used android:noHistory="true" in the manifest of the launcher Activity of my app and the problem gets solved now.

Android Intent.FLAG_ACTIVITY_SINGLE_TOP AND Intent.FLAG_ACTIVITY_CLEAR_TOP

I have an app that I have running a media player and I want to resume the activity from my apps home activity.
I can successfully do this by adding the following flags to the startActivity Call:
myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
I am worried that this is not an ideal way to do things since it took me a long time to find it. It made me think that no one uses it very much.
Are there any pitfalls to using this method?
I know that this question is quite older and may you have solved your problem and may be travelled to mars and back in those years.But just to make things clear for the people coming here and looking for an explanation:
According to Official Documentation:
If set, the activity will not be launched if it is already running at the top of the history stack.
Suppose you have BackStack of A-B-C-D and You have launched
another intent Starting Activity A with FLAG_ACTIVITY_CLEAR_TOP
at this point what android will be do is Simply Clear All the
Activities on the Top of Activity A, means now your BackStack Will
Look like-> A (yes that's it because you have cleared all the
activities on top of it).
And In Second Scenario Suppose You have the same BackStack A-B-C-D
and you launched an Intent Starting Activity A with
FLAG_ACTIVITY_SINGLE_TOP, now your BackStack will look like->
A-B-C-D-A (Confused? Don't Worry Next Example Will Clear All Your
Confusions)
Third Scenario, We start with the same BackStack A-B-C-D and We
will launch an Intent Starting Activity D with
FLAG_ACTIVITY_SINGLE_TOP, now our BackStack will look like->
A-B-C-D (Yes the BackStack remains in the Same Order because our
FLAG prohibits the launch Same Activity If it is the Currently on Top
of the Stack and Showing on the User screen.
Last Scenario, We start with our same BackStack A-B-C-D and We will
launch an Intent Starting Activity D but this time with no FLAGS,
now our BackStack will look like-> A-B-C-D-D.
Got it? FLAG_ACTIVITY_SINGLE_TOP is only useful When you are
starting the Same Activity Which is Currently Showing On the Screen
and You want to prohibit the launch new Instance of the Existing Activity.
Just to make sure I understand your question correctly: Say your activity stack is something like A -> B -> C, and now from C you want to clear the stack and get back to A.
If this is what you want, then those intent flags are correct and should work as expected, as per the Android-developer docs.

Categories

Resources