Having multiple fragments at one screen with Jetpack Navigation component

Viewed 1404

Two fragments

I'm using navigation component and trying to understand is it possible to have two or more opened fragments simultaneously like mail app (one fragment is a list of mails, another fragment is detailed mail view). It's desirable to have them inside another container parent fragment.

1 Answers

Yes, it's even recommended to design your app for multiple screen sizes. You can do so by creating alternative layouts, e.g:

res/layout/fragment_items.xml          # Screens smaller than 600dp available width
res/layout-w600dp/fragment_items.xml   # Screens 600dp wide and bigger

where layout-w600dp/fragment_items.xml includes both fragments. This way you keep the same navigation destination and don't need to complicate things with conditional navigation.

It's desirable to have them inside another container parent fragment.

Depends. If you are going to use your list-fragment on multiple places, yes, because it would reduce duplicate code. If not, or your list-fragment is going to be different on multiple screens (you might add or hide some buttons) you don't need the container; and can just modify the fragment_items.xml layout for different screen sizes.

Related