Why is it recommended to use only the default constructor to create a Fragment?

Technology CommunityCategory: AndroidWhy is it recommended to use only the default constructor to create a Fragment?
VietMX Staff asked 3 years ago

In short, Fragments need to have a no-args constructor for the Android system to instantiate them. Your Fragment subclasses need a public empty constructor as this is what’s being called by the framework.

It is used in the case when device has to restore the state of a fragment. No data will be passed and a default fragment will be created and then the state will be restored. Since the system has no way to know what you passed in your constructor or your newInstance, default constructor will be used and saved bundle should be passed via onCreate after the fragment is actually instantiated with the default constructor.