I'm trying to add a view to the fragment stack, without any visible indication that it happened, so it appears when going back.
Forexample,
3 Fragments, A, B and C
A -> C
Then when going BACK from C, I want B to appear (i.e. we 'skipped' over it)
C -> B -> A
My issue is these all need to be backstack + animation compatible.
I can show B by hijacking the back event, and doing a fragment transaction to replace C with B (and invert the animations so it 'enters' as if the C popped), but then going back again reverses that so I get stuck in a C -> B -> C -> B loop.
I try calling PopBackstack first before doing the transaction, but then C just 'vanishes' as B enters - but then going back from B -> A works fine.
- Alternatively, is it better to somehow add B + C simultaneously when navigating from A -> C and if so, how best to do that so the backstack is fine too? Baring in mind, B needs to not be visible so visually its animating from A -> C.
- Otherwise, any ideas on injecting a fragment into the stack when going back this way?
And no, Compose isn't an option.
Here's example pseudoish code of what I've been trying. I'm not trying to show B + C at the same time such that it appears you navigate A -> C.
Activity
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
So first I'm showing fragment A which is shown with the following rough transaction
var fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.setCustomAnimations(slide_left_in, slide_left_out, slide_right_out, slide_right_in)
.replace("@id/content_frame", fragmentA)
.addToBackStack("A")
.commitAllowingStateLoss();
Its not 100% on the above as I'm deep in various other views by this point but basically, FragmentA is currently the active fragment in content_frame and is working fine.
Now, I want to navigate to C but with B 'hidden' in between. So as suggested by /u/ianhanniballake I'm doing them as two transactions (so they are both on the backstack). This however is not working currently...
fragmentManager.beginTransaction()
.setCustomAnimations(slide_left_in, slide_left_out, slide_right_out, slide_right_in)
.replace("@id/content_frame", fragmentB)
.addToBackStack("B")
.commitAllowingStateLoss();
// If I don't do this, the second transaction appears to never execute so just FragmentB appears
fragmentManager.ExecutePendingTransactions();
fragmentManager.beginTransaction()
.setCustomAnimations(slide_left_in, slide_left_out, slide_right_out, slide_right_in)
.replace("@id/content_frame", fragmentC)
.addToBackStack("C")
.commitAllowingStateLoss();
What I essentially see is FragmentA gets destroyed and vanishes before its animated out, so I then see Fragment C slide over 'nothing', but then going back works fine seeing FragmentB then FragmentA.
EDIT: Ok, so changing FragmentB to an 'Add' instead mostly works. But this does mean the 'pop' of FragmentB doesn't animate FragmentA, its just static (as it was added I guess, not replaced). Not sure if theres anything I can do to resolve that? If I do Replace, FragmentA vanishes due to the second transaction, if I do Add it works, but visually doesn't animate (slide) correctly when popping back.
1 Answer 1
Why not jump to B directly and remove fragment C, then when you remove B it will back to fragment A.
Explore related questions
See similar questions with these tags.
addToBackStackis a property on a FragmentTransaction, then it needs to be two separate FragmentTransactions. Again, if you show what you've tried, we can take a look and offer an answer that does what you want.