Home > database >  How to replace fragment outside of bottom sheet by clicking button inside of bottom sheet?
How to replace fragment outside of bottom sheet by clicking button inside of bottom sheet?

Time:07-03

So I've decided to make my app with single Activity. In one of my fragments (fragment(A)) I open bottom sheet by clicking Button open BS. Now the tricky park, inside bottom sheet there is a FragmentContainerView that has two fragments inside and I want to click Button B inside this FragmentContainerView (in fragment(B)) that would change fragment(A) to fragment(C) and close bottom sheet.

I'm aware that by using code below it's possible to swap fragment and close bottom sheet (here I'm clicking button outside of FragmentContainerView but still inside bottom sheet):

ButtonSH.setOnClickListner{
    parentFragmentManager.commit {
          // replace fragment
          replace<FragmentC>(R.id.fragment_container_that_holds_fragmets_A_and_C)
    }
    // close bottom sheet
    dismiss()
}

But I just can't understand how to do the same by clicking Button B inside FragmentContainerView in fragment(b).

If it matters, fragment(A) is not directly inside activity, it is inside a few other fragments.

Here is an image for better understanding:

visualisation for better understanding

CodePudding user response:

So my solution was to make sharedViewModel for bottom sheet and its fragments, and to use another sharedViewModel for container that holds fragment(A) and for bottom sheet. I observe variables inside viewModels to replace fragments. For official documentation visit this

  • Related