0

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.

asked Nov 8, 2025 at 16:00
5
  • 1
    Adding B + C at the same time is exactly what you should be doing and fully supported (without ever showing B). Show your code and what you've attempted. Commented Nov 8, 2025 at 22:09
  • What would the fragment transactions be to add them.both? Would I do two separate Replace transactions committing one at a time, or an Add and then Replace? Or can I do two replaced in the one transaction? Will the visually show A -> C? Commented Nov 8, 2025 at 23:32
  • If they need to be two separate entries in the back stack and addToBackStack is 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. Commented Nov 9, 2025 at 0:19
  • I'll try adding code, its going to be fairly cut down but hopefully shows what I've been trying to do... Commented Nov 9, 2025 at 20:39
  • I've added code and I'm very close now. I'm doing an Add transaction showing fragment B, then immediately doing a Replace transaction for Fragment C. This all looks good going A -> C. This all looks good going back C -> B. However going back B -> A, B animates out but A is static when it appears. If I do a Replace showing fragment B. Going from A -> C fragment A just vanishes instantly. This looks good going back C -> B. This looks good going back B -> A. (sorry, hate SO comment formatting with no new lines) Commented Nov 10, 2025 at 9:06

1 Answer 1

0

Why not jump to B directly and remove fragment C, then when you remove B it will back to fragment A.

answered Nov 10, 2025 at 9:03
Sign up to request clarification or add additional context in comments.

Not sure I follow how that would work. Do you mean do a Replace of B on A and then an Add for C? Would I not have the same issue but with B remaining static on pop enter?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.