Why do I want to avoid non-default constructors in fragments?

It seems like none of the answers actually answer “why use bundle for passing parameters rather than non default constructors”

The reason why you should be passing parameters through bundle is because when the system restores a fragment (e.g on config change), it will automatically restore your bundle.

The callbacks like onCreate or onCreateView should read the parameters from the bundle – this way you are guaranteed to restore the state of the fragment correctly to the same state the fragment was initialised with (note this state can be different from the onSaveInstanceState bundle that is passed to the onCreate/onCreateView)

The recommendation of using the static newInstance() method is just a recommendation. You can use a non default constructor but make sure you populate the initialisation parameters in the bundle inside the body of that constructor. And read those parameters in the onCreate() or onCreateView() methods.

Leave a Comment