Search code examples
androidkotlinnavigationfragmentandroid-navigation

How to make bottom sheet dialog fragment being opened from all the fragments and go back to the same fragment in Android [Kotlin]


I want to start the bottom sheet for login from different fragments in my app. The issue I am facing is handling the back button on the bottom sheet fragment. I am not able to pass the directions for going back to the fragment from where I triggered this bottom sheet fragment.

What is the right way to handle this? How can I trigger bottom sheet fragment from any activity or fragment in my app and go back to the same fragment from where it started?


Solution

  • Fixed this issue by using Navigation properly.

    Using navigation's direction I defined multiple directions to the login fragment and made it to receive the "Fragment Name" as argument. Now this fragment knows which other fragment ( A or B ) it is triggered from.

    So on pressing back just used the direction based on the fragment it came from.