0

I'm looking for the answer to this question: How to implement a nested master detail flow on Android?

Looking for example code. Essentially I'm trying to implement a master-submaster-detail layout (master list, click item brings me to submaster another list, click item brings me to details of that list (this details view will have comments hence another list), and from detail view, I can navigate up to submaster and up to master. This is all in ONE tab for the navigation, I can click on another tab and it brings me to a different master/detail fragment

In my MainActivity, I have four master fragments that I go to via a BottomNavigation bar. For each master fragment, clicking on item in the RecyclerView will take me to a detail fragment. In the detail fragment, I still see the BottomNavigation bar (on purpose because I thought this would easier to navigate so user do not need to navigate all the way up to the master fragment in order to change tabs to another master fragment). However, when I am in the detail fragment and I click the BottomNavigation tab to another master fragment, and in that other master fragment I try to click on item in the recyclerview to its own detail fragment, it does NOT work. I must've implemented this navigation wrong.

For example: I have tabs for FragmentA and FragmentB. Currently I am in FragmentA and I see a RecyclerView. I click on the Xth item in FragmentA RecyclerView. This works fine. I click the tab for FragmentB. This also works fine. I now try to click on Yth item in FragmentB, this does NOT work. It only works if I click back, THEN click on Yth item in FragmentB, then it work.

What's the problem?

waynejohn1110
  • 111
  • 1
  • 8
  • how are you switching fragments? and how are you navigating to the details view? @waynejohn1110 – N Jay Dec 19 '17 at 00:11
  • Right now, I use one FragmentManager, I call this fragmentManager to go to the master fragment from my MainActivity, by clicking on item in recyclerview of master fragment, it calls the same fragmentManager to go to submaster fragment, and I click the item in recyclerview of submaster fragment, and it calls the same fragmentManager to go to the detail fragment – waynejohn1110 Dec 19 '17 at 00:18
  • how are you switching the fragments ? using replace? or add or what exactly ? – N Jay Dec 19 '17 at 00:21
  • using replace. And I add the master fragment to backstack using addToBackstack – waynejohn1110 Dec 19 '17 at 00:24
  • okay first change the way you do things a little. – N Jay Dec 19 '17 at 00:26

1 Answers1

0

First of all i would change the way you are doing things.

The fragments in your tab navigation should keep them as different fragments .. but for every one of those master fragments use childFragmentManager to handle the like submaster and details.

So you will have supportfragmentmanager used to manage all of your master tab fragments and you will have childfragmentmanagers to support all the children within those masterfragments.

Try this approach out.

N Jay
  • 1,774
  • 1
  • 17
  • 36
  • First, I can't fnd getchildfragmentmanager. I'm calling this in a onClick listener, where on click item in recyclerview will call fragmentmanager to the submaster fragment, however the getChildFragmentManager is not existing. It says online I should extend the parent Fragment. This is because in my OnClick method, only a view is passed in, and I can't cast it into a Fragment, I can only cast the view.getContext() as an activity, and the activity (which is FragmentActivity) cannot call the getChildFragmentManager – waynejohn1110 Dec 19 '17 at 03:44
  • Well then make sure the context that ur using is of a fragment and not of a fragmentactivity. – N Jay Dec 20 '17 at 01:50