0

I want to enforce the factory pattern for certain Fragment, since according to this question, it's the way to go for passing data to Fragments.

However, to enforce it, me must prevent instantiating the fragment. The way to do this is making the constructor private, according to this question.

The problem is that I get this error: could not find Fragment constructor

According to this question, All Fragment classes you create must have a public, no-arg constructor.

So apparently I cannot make the constructor private. So how can I enforce the Factory pattern?

mathematics-and-caffeine
  • 1,664
  • 2
  • 15
  • 19

1 Answers1

0

You can't enforce it by compiler because a fragment have to be public to restore fragment manager's state.

You can add a custom lint/detekt rule to your project which will fail build if Fragment's constructor called every where except the fragment's factory.

Viacheslav Smityukh
  • 5,652
  • 4
  • 24
  • 42