Docs say you shouldn’t nest fragments ’cause they are designed to work another way. But!
Scope
But what if I use ViewPager and it’s fragment (visible screen) should be constructed from other fragments. Let’s say we have a messenger app. On the main screen there’s a ViewPager with fragment called "Last messages" (MessagesFragment) and on the submain screen (let’s say Discussion or Chat room) we also have a ViewPager but Messages screen consists of MessagesFragment, NewMessageBarFragment and e.g. header fragment with other info.
Problem
One can say we shouldn’t use ViewPager for that and we can use ViewFlow open source library but believe me the code, holded in such activity is a mess even with fragments. Also there’s an approach to fill extra layouts, not fragments for NewMessageBar and HeaderInfo within MessagesFragment — but it’s double ugly as for me.
Question
Is it OK to use nested fragments in this particular situation? What would you recommend?
Upadate
Sometimes it’s really ‘must have’ feature. But! In most cases I would recommend follow ‘responsive design’ way: http://www.slideshare.net/kirillcool/responsive-mobile-design-in-practice
Don’t do that.
Absolutely not.
Replace your “outer” fragments with simple layouts. Then use some other
PagerAdapterimplementation that does not itself require a fragment (as does theFragmentPagerAdapterinterface). You may need to pinch some code fromFragmentPagerAdapterto make sure this all works well.Or, combine “MessagesFragment, NewMessageBarFragment and e.g. header fragment with other info” into a single fragment for use on screen sizes where you want to have this pager behavior. This may result in some amount of code duplication, to have that portion of the UI broken out separately for smaller screen sizes.