Manipulating back task stack in android - 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-

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.

Android resume from one fragment activity to another and vice versa

I want to create Android app where there are 3 main FragmentActivities, A (main), B, and C, also there are other sub activities. this app can go from activity A to activity B or activity C freely, and vice versa without starting new activity unless it is not started or finished. so how i can achieve this?
----- Edited -----
You can't. At any point in time there is only one Activity that is active. They will be finished and they will be garbage collected.
What you can do is set your launch mode to single task and see if it helps in your situation.
Use Navigation-Drawer or Swipe View..fragments are your solution. So you'll have FragmentActivity and 3 fragment A B C to replace
I will move activities layout to views, and write within one Activity, use hide/show to change to A, B, C.

Get activity from Back Stack keeping history, instead of creating new one

I'm trying to do something like this:
I have some activities in task:
A->B->C
and from C i want to call instance of B from stack and keep current history. So the wondered result looks like this:
A->B->C->B
where B and B is the same instances, but with possibility to navigate back with Back button.
Is it possible?
May be duped:
Android task history stack: can have "duplicate activities"?
As I know Android doesn't support this case.
You can have either:
A->B->C->B (where B are both activities of the same class, but which are two different objects)
pr
A->C->B (where B is brought to the front).
However, I think you can build something on your own. If you will serialize the state of B on onPause() and deserialize it on onCreate() and onResume(), you may have two B's in the stack (which will be separate objects), but they still have the same state (as example all members).
I solved my problem by realising my own Back Stack. Main idea was to use FLAG_ACTIVITY_BRING_TO_TOP for indicating my singleton acivities.

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.

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

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.

Categories

Resources