0

I have a setup like the navigation graph below. The 3 fragments in the middle are very similar and all extend a BaseFragment class.

enter image description here

What I would like to do is to make BaseFragment abstract, so that my navigation graph can be reduced to the figure below.

enter image description here

Is this even possible? It would drastically reduce the clutter of my navigation graph, because there will eventually be upwards of 20 children. However, I think this improvement would require me to instantiate an abstract class, which isn't possible.

Daniel C Jacobs
  • 691
  • 8
  • 18
  • You're looking for arguments, not an abstract class – EpicPandaForce Sep 17 '21 at 15:53
  • I don't quite understand how that solves the problem. Are you saying use the second picture, but make `BaseFragment` non-abstract and just pass it an argument to indicate which child to use? Each child fragment uses the same xml template and inserts slightly different views into the template, so I need to ensure that I can populate my views correctly. – Daniel C Jacobs Sep 17 '21 at 16:55
  • Sounds like they don't need a common base then. – EpicPandaForce Sep 17 '21 at 20:18
  • They definitely benefit from a common base. They all do the same functions, they just vary slightly in the implementation. If I eliminate the common base, then I have more duplicate code than unique code. – Daniel C Jacobs Sep 17 '21 at 20:35
  • Sounds like a usecase for a compound viewgroup or a common "controller" of sorts, but doesn't sound like the behavior should be coming from a common parent fragment. – EpicPandaForce Sep 17 '21 at 20:46

1 Answers1

0

No, a Fragment cannot be abstract.

The solution I decided upon was to have a helper class that stores the data I want to display in my Fragment. This helper class has a child for each of the original ChildFragment classes.

I can dynamically population my Fragment by simply reading the data stored in this helper class, thus allowing me to implement the second figure in the question.

Daniel C Jacobs
  • 691
  • 8
  • 18
  • `No, a Fragment cannot be abstract.` How do you come to a conclusion like this? A Fragment can certainly have an abstract BaseFragment class, and that is what I am doing in my project right now. – arne.jans Jan 11 '23 at 14:20