Hide all Fragments in Backstack but still being able to go back - android

I have a small layout in my activity that I add Fragments to based on the User navigating through the app.
Assuming the user navigates thusly:
Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click
I would like to be able to to hide the Fragments and show the blank Activity again.
This is how I'm adding the Fragments to the activity:
protected void addFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
}
To clear all the Fragments, I use:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?

Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?

Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.
HTHs

Related

Update viewpager fragment specific position and get back to previous while backpress

I have a view-pager with two fragment in it . based on my logic i want to replace the fragment on a specific position with another fragment but when user back-press i want to go back to previous fragment i replaced with it
for clearance
i have 4 fragment A, B, C, D initially i added A and B to view-pager, then i replaced B with c in view-pager , So that i want is to go back to fragment B (replaced by C) from C (current) while pressing back button instead of going back to another activity
you can do using assign constant variable to your fragment or assign tag at time of Fragment Call.
I manage using VariableName CurrentFragment which update value on Fragment Lunch. Then after onBackpress method of your activity i handle the fragment based on Constant Variable.
Visit below Stackoverflow link where i put solution step-wise to handle multiple fragment on backpress.
Click Here to View StackOverFlowAnswer

Restart fragment after back button

I have several fragments that share the same container: Fragment A, Fragment B, Fragment C. I move through them with the .replace method and I always use .addToBackStack to keep them in the stack.
At the current moment, if Fragment C is displayed and I click the Back Button to go back to Fragment B the methods from onCreateView() of Fragment B onwards will be called, but not onAttach or onCreate. I would like to restart Fragment B completely after I click the Back Button so that these methods are also called. In other words, as if I had gone from Fragment B to a different Activity and then back to Fragment B.

FragmentA (pressing next)--> Activity(automatically starts)--> FragmentB

I have the following scenario:
FragmentA (pressing next)--> Activity(automatically starts)--> FragmentB.
Because of the Android library that I'm currently using, I must start my fragmentB automatically through an activity.
I tried android:noHistory="true" on my activity but I still have the following unwanted behavior:
Current behavior: When pressing back button on FragmentB, I'm going to my activity, then when I press it again I go to FragmentA.
My activity only has a toolbar and nothing else.
I would like to go back to FragmentA when pressing the back button on FragmentB
getActivity().finish() ;
getActivity().overridePendingTransition(0,0);
will kill the activity along with the fragment and make the transition instant.
Try adding the fragment to the backstack while performing the FragmentTransaction.
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
.addToBackStack() // Add this transaction to the backstack
.commit();
when using fragments in your app, individual FragmentTransaction objects may represent context changes which can cause loosing the previous fragments and required to be added to the back stack while performing FragmentTransaction. To understand the back navigation in android in better way and detailed explanation you can refer this

What does this code mean exactly (involving popBackStackImmediate and pressing back)

This snippet of code:
#Override
public void onBackPressed() {
if( !getFragmentManager().popBackStackImmediate() ) super.onBackPressed();
}
What does this mean exactly? I looked in the docs and it says
"Like popBackStack(int, int), but performs the operation immediately
inside of the call. This is like calling executePendingTransactions()
afterwards."
But I don't know what this means, or what it means to have the negation in front of it, or what super.onBackPressed() is doing.
Returns true if there was something popped, else false.
Basically you are popping your backstack (cleaning the fragments on the history, so when you go back you will not go to the last fragment) and if theres no fragment on the backstack (no fragment on the history) you will do your super.backPressed.
The super.onBackpressed() will execute your activity onBackPressed (even if this code is called from a fragment, as there is no default code to do on your fragment onBackPressed().
If your activity extends a default Android activity it will go back to the last activity, if theres one, os get out of the app, if theres none.
pseudocode:
popBackStack() // clear the fragment history
if (somethingWasPopped) {
super.backPressed() // this will do what i said above
}
Probably the developer who done this was trying to make the user unable to get back to the last fragment when he press back. Like Activity1 -> Activity 2 (frag A -> frag B -> frag C). Now, user is on frag C, he will get back to Activity 1 (when he press back or click on back icon), instead of frag B.

Android - How to avoid onResume of fragments when popping them from the backstack

We have multiple screens in our app and to switch between them we use FragmentTransactions, primarily replace, and we add fragments to the backstack when we do so. While doing so we stay within the same MainActivity.
So if we transition from Fragment A to Fragment B via some button we now have a stack that looks like
B
A
If we go to another Fragment C it goes on top of the stack like so
C
B
A
However, sometimes we transition to a fragment D, such that we want to wipe out the backstack so that users can no longer navigate back through C, B, A. We now want our backstack after navigating to D to look like this.
D
But in order to do so we need to clear the backstack using:
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
while(supportFragmentManager.getBackStackEntryCount() > 0)
{
supportFragmentManager.popBackStackImmediate();
}
clearingBackStack = false;
But when we do so, the onResume of fragments C,B and A are called. This is not the functionality we intend and can have negative consequences such as making unnecessary server calls. We have also noticed on low end devices that we see a flash of the popped fragments as they come off the stack.
We wish to avoid this behavior, is there a way to pop the entire fragment backstack without the popped fragments being activated in anyway?

Categories

Resources