Android how to trace and clear certain backstack - android

My Activity backstack structure looks like this:
A(Landing page) -> B(List screen) -> C(Details screen)
|----> D(Post
item screen) -> E(Post result screen)
Both B & D are direct child of A.
I wanted to provide a way for user to view the detail(C) from the result screen(E). While they go away, I wanted to remove D & E so that they can't go back when pressing back. Once jumped when user press back from C then they should go back to A instead.
EDIT: However, user can press back on E screen to go back and make change in D screen to fix whatever mistake they made so I kinda have to keep D in backstack as well.
What is the best way of doing this? Should I prevent D&E from ever going into backstack and handle my own navigation? Is there a way to clear D & E specifically and keep everything else? Or is there a better way to do this?
I considered using Clear Top flag but if user never visits C before Clear top wouldn't work as you can't go back to A afterward.

I use Activity.finish() to hide a Activity from the backstack.
As an example:
The user sees activity B and starts activity C. After stating of C call finish to hide B from the backstack:
startActivity(<Activity-C intent>);
finish();

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.

Restoring fragment backstack for fragment with two possible backstack paths

I have one Activity with 3 fragments which form a workflow to collect user input.
Normally, Fragment A is the first fragment -> Launches B -> Launches C. B is supposed to launch A if the back button is pressed, and similarly C's Back button is supposed to launch B.
However, in some cases, A is supposed to launch C directly, and then C's back should launch A again.
I prefer that C should not know who launched it. I.e. I want C's "backstack" to operate without C knowing who launched it.
I tried using the usual addToBackstack approach, but I'm facing a problem when the Activity gets killed after the user lets the app go into the background while C was open.
I would like the user to return to "C" instead of starting all over from A. To achieve this I'm using the saved Instance state, and detecting which fragment was previously active, and launching it from the activity.
The problem starts when the user wants to go back from C, after the Activity was recreated after being killed. C doesn't know who launched it: A or B. How do I make the Back button work as expected for this case?
Found some answers in this excellent video by Android Developers + Adam Powell:
Fragments: Google I/O 2016
In summary, Fragments and the Fragment BackStack are considered part of the navigational state of the app, so, as long as I don't mess with the backstack when the activity is launched, the OS will also restore the FragmentBackStack, thus the BackStack will know who launched C (A or B) even if the activity gets re-created. Thus, popBackStack will move from C to A or B as required.

Return to the original activity after starting a new activity

I have an Activity A and I call Activity B. The problem is if I navigate away from B and come back to B again, after this once activity B is finished, I am redirected to the Android Home screen rather then the original activity A. I need to find a way to get back to A. Here is the complete flow:
A -> B -> (Navigate from B to some activity or home screen) -> (come back to B) -> (B finished after calling finish()) -> (Android Home screen is shown)
Expected result:
A -> B -> (Navigate from B to some activity or home screen) -> (come back to B) -> (B finished after calling finish()) -> (A should be shown)
I understand this is due to the way in which Activities are stacked and since I navigate from B the Android Home screen becomes the top of the stack and that is why A is not shown. However is there a way in which I can manipulate the top of stack to come back to A. I tried Intent flags and its combination but it did not work for me. I may have missed something if you can help or is there any other way to do it?
It seems like finish() is called for Activity A, that's why it's not displayed when you want it. First step is to check if you're accidentally calling finish() yourself.
Also keep in mind that it's possible that the system has killed (finished) Activity A. This can happen if there's big need for memory ... for example if your Activity B is super heavy (e.g. doing lots of Bitmap transformations) it could be just that.
Lastly, if possible you can specify parentActivity of B to be A, by adding this in your manifest android:parentActivityName="....activity.A". This way Android will open A when you back from B.

Always have Activity on bottom of back stack

I have an App with a Navigation Drawer design and some activities (I know working with Fragments would probably be easier here but that wasn't feasible).
When the user clicks a drawer item, the according Activity is started with FLAG_ACTIVITY_REORDER_TO_FRONT. The up button should always take the user back to the start activity A, so when the up button is pressed I also start the A activity with the same flag.
Consider the following (capital letters are the activities, trying to visualize the backstack here):
A
user starts B from the drawer
A -> B
user presses up button, brings A in front
B -> A
user presses back button finishing A going back to B
B
user presses back button again which exits the application, but imho the user should always see the start Activity A again before leaving the app.
How would / did you guys solve this?
EDIT: #Neil, with that is: If I have
A -> B
and from there via drawer go to C I have
A -> B -> C
If the user now presses the Up button and I just close C instead of bringing A to the top, the user will be back in B which would be wrong because C is (navigationwise) not under B but they are 'siblings'.
Try to make the launchMode of that Activity B to SingleTask in your manifest file like this :
android:lanchMode="singleTask"
Your activity will not be called again.

Android back button and resume activity

I have this issue.
I have an actvity A that starts other activity B (by onclick - button).
In B I have one back button to come back to activity A. I press it.
Now that I'm in A, I press again button to go to B.
If I use android back button (I'm in B) I come back to A first and then to B.
But now, if i press android back button again, I don't go to previus activity of A or it exit from app. I come back to B !!!
How can I prevent this behavior ?
The back button of activity B should not start activity A, but close activity B with finish:
http://developer.android.com/reference/android/app/Activity.html#finish%28%29
Note that it is probably bad user interface design to have a "back" button on the interface. This official Android page says "Don't use labeled back buttons"
http://developer.android.com/design/patterns/pure-android.html
if you wish your custom back button to swap between the acivities A and B, you should simply launch intents, that will do.
I believe your problem is with the android back button. If you do not want go to the previous activity with this action, you can write in the manifest file under the <activity> tags for both A and B activities - android:noHistory="true". Doing this will exit from the app. as the activity stack was storing none of the activities, but such a requirement is quite confusing UI approach.

Categories

Resources