Search code examples
androidandroid-fragmentsbottomnavigationview

Single vs multiple fragment containers


I am using a bottom navigation bar in my project with MVC architecture. I am used to creating projects where I use only one fragment container (usually I use MainActivity.kt/main_activity.xml), switching my fragments in and out depending on user interaction.

Now since I am using a bottom navigation bar that is a part of main_activity.xml (which I use as my fragment container) which I think should be used for all those fragments that contain bottom navigation bar, and not for all the other fragments that are not a part of the bottom navigation bar.

Should I create a second activity/xml, as a 2nd fragment container, for all my fragments that don't contain bottom navigation bar (for instance, pressing a button that opens a new fragment that has nothing to do and doesn't contain a bottom navigation bar)? Or should I try to cram all that into a single container? What would be the best approach for this particular situation?


Solution

  • In my humble opinion, I think you don't have to create another activity for your non-bottom Navigation Fragments. It gonna be OK if you used the same MainActivity for that. My other recommendation is to use Navigation Component, make one NavHostFragment act as your fragments(destinations) container.

    Explore this article to implement Navigation Component with bottom navigation.

    Also, this article will let you know how to transfer between fragments.