Reputation: 2480
I have a sole activity for a lot of fragments. In one of the fragments, I must now have a few tabs inside. The ways I can think of doing it is are:
Solution 1 - Creating a new activity just for that, and then implementing ViewPager :
My main concern regarding that solution is this: So far, on my Activity , I had the following flow : fragment 1 -> fragment 2 -> fragment -> 3 -> fragment 4 -> and on and on...
Now, suppose I have to implement those Tabs in fragment 3.
from fragment 2, I start a new activity placing a viewpager (with fragment 3 in one of the tabs and other ones in the other tabs). Then I need to be able to show fragment 4 . But since all the fragments in the app are placed on the original activity, it would mix up the flow or even worse. In other words cause either:
Solution 2 - Activity for result (with view pager):
The activity created for tabs screen could be an activity that results back to the original activity. so the flow would be like this: fragment 2 -> new activity for result -> send result back to original activity stating that it needs to fire up fragment 4 now -> original activity opens fragment 4.
However, in that case if I go backwards from fragment 4, it would throw me back to fragment 2 instead of that tabs screen(3).
Solution 3 - Tab Layout :
Seems as the best solution.
However, to use that I need getChildFragmentManager()
, which requires api 17 and above. My api is 16.
Now, if I use getSupportChildFragmentManager()
I would have to use fragment v4. The problem with that is that all my fragments are native, and I can't just change that fragment 3 to be v4, cause then the previous ones and the following ones would have to be v4 as well. Changing all the fragments is not an option since I have about 50 fragments.
Another issue with solution 3
I've encountered , is that it crashes and prints : Error inflating class android.support.design.widget.TabLayout
I tried creating a new folder named values-v21, and created there a styles.xml for the tab layout, but that didn't help for some reason.
I hope I made it clear.
Upvotes: 2
Views: 877
Reputation: 87064
You have another solution derived from solution 3. This is to keep your current navigation(fragment1->fragment2->etc) and build a fragment to hold the ViewPager with simple layouts instead of nested fragments. This way you'll avoid: navigation issues, the problematic nested apis not being present on version 16(which you plan to support) and you'll also avoid a refactoring to use the support fragment api.
And if at a later time you drop support for version 16 you could refactor each of those simple layouts in a nested fragment and use the native getChildFragmentmanager().
Upvotes: 1