Fragment transition using add and replace - android

I am having a situation Fragment A is used Add but in FragmentB I used replace for FragmentC.
FragmentA--Add---FragmentB---Repace --FragmentC
When i clicked back from fragment C it brings me back to Fragment A.
Note : all fragments added in backstack
Required behavior : When pressed back from fragmentC it must come back to FragmentB. Instead it brings back to fragmentA
Plz help.
Regards

Related

How to show action bar when replace a fragment after hiding it on previous fragment?

I have an actionbar on MainActivity level (mainActionBar) and an actionbar on fragment level (fragActionBar). I have 3 fragments (A,B,C). I want mainActionBar to be shown when I'm accessing fragment A and B. On the other hand, I want fragActionBar to be shown when I'm accessing fragment C.
I use replace() when changing between fragments and use addToBackStack to save previous fragment so I can use popBackStack to back to previous fragment. The flow is like this : A<->B <->C. fragActionBar contains up button and if I press the up button it will back to fragment B. mainActionBar doesn't have up button, I use button outside toolbar/actionbar to change fragments between A<->B->C. So only C->B that is using up button.
I have succeeded to show mainActionBar when I'm on fragment A and B. I also have succeeded to show fragActionBar when I'm on fragment C. But when I go back to fragment B, the mainActionBar didn't show up. I try to put supportActionBar?.show() inside onResume(), but it didn't works. I also try to put (activity as AppCompatActivity).supportActionBar?.show() inside setNavigationOnClickListener, still didn't works.
How can I show the mainActionBar when I get back to fragment B after visiting fragment C?
Turns out, I need to use the toolbar I declared in MainActivity by passing the binding from MainActivity to other fragment. It is because I use a different toolbar in other fragment, that's why it won't affect the changes even after I use .show().

Clearing specific fragments from backstack

I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!

Close the currrent fragment and open the previous one

Imagine i have a frameLayout in my main activity, and two fragments,
so now first fragment is being displayed, i replaced it with secondFragment and added first one to back stack, now some condition is met in the secondfragment so i want to close the second fragment and open the first one where we left that (basically back from the back stack) without pressing the back button. how do i achieve this ?
you can write this code for removing current fragment from backstack:
getActivity().getSupportFragmentManager().popBackStack();

Fragment navigate back without adding to back stack

This was asked in one of the interviews. The scenario is described as follows :
MainActivity (initially has Fragment A added to it.). On pressing a button, it goes to Fragment B, without adding A to backstack. On pressing a button in Fragment B, it adds 'B' to back stack, and goes to Fragment C. Now, in fragment C, I have a button through which I directly want to navigate to fragment A (which wasn't added to back stack). Is there any way possible to do this?

Can't see added fragment on back button press

I have an activity. When it starts it loads Fragment A in onCreate using replace. This is not added to the back stack.
I then want Fragment B, an overlay, to be added on top of the previous fragment. This is because i still want you to still be able to see Fragment A behind it as Fragment B has a translucent background. This is loaded using add and is added to the back stack.
From Fragment B i load Fragment C using replace and add to the back stack. This is because Fragment C is not an overlay like Fragment B. When i'm in Fragment C i want to go back to Fragment B on back press. I override on back press and pop the back stack.
The issue is when i press the back button i see Fragment A and not Fragment B. Even though i have debugged and can see Fragment B in the back stack. Why is this?

Categories

Resources