Error Non-Default Constructors in Fragments

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

Make a bundle object and insert your data (in this example your Category object). Be careful, you can't pass this object directly into the bundle, unless it's serializable.
I think it's better to build your object in the fragment, and put only an id or something else into bundle. This is the code to create and attach a bundle:

Bundle args = new Bundle();
args.putLong("key", value);
yourFragment.setArguments(args);

After that, in your fragment access data:

Type value = getArguments().getType("key");

That's all.

How to ignore the avoid non-default constructors in fragments error?

Found the solution. The easier way is to add these to the gradle:

 android {
lintOptions {
checkReleaseBuilds false
}
}

Or another way is to add @SuppressLint("ValidFragment")

Error: Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle) instead [ValidFragment]

This is a good strong suggestion by Android Studio. The reason is "constructors will not be called when the fragment is re-instantiated". Instead setArguments(Bundle) will. This is according to Google webpage @ Fragment.
Do you have code for Bundle passing?

In Android framework, override methods like onCreate and onCreateView are reinstated, not constructors, like when user changes orientation.

How to Resolve This Fragment non-default constructor Error?

I am confused why public BasicInfoFragment() is okay but the other two are not?

When an activity is destroyed and recreated as part of a configuration change (e.g., screen rotation), Android automatically destroys and recreates the fragments that were managed by the old activity instance. To do that, it will use the default constructor, not either of the other two constructors.

Also, when your app's process is terminated while you are in the background, and the user returns to your app (e.g., recent-tasks list), Android will create a brand-new instance of your activity... and all of its fragments. Once again, it will use the default constructor.

The risk of using the other constructors is that any objects passed in to those constructors is prone to being lost during either of the above events. Lint is steering you towards getting rid of those constructors and using the factory pattern (e.g., a static newInstance() method), where data is supplied to the fragment via the setArguments() method. This attaches a Bundle, retrievable by the fragment instance via getArguments(). That Bundle is automatically part of the saved instance state, which will be held onto through either of the above events.

TL;DR: the other public constructors represent a strong enough "code smell" that Lint nowadays yells at you

How to change non-default constructor in fragments to default constructor?

When you create your fragment, use setArgument():

Bundle args = new Bundle();
// Construct your bundle here
Fragment mFragment = new PlacePickerFragment();
mFragment.setArguments(args);
mFragment.initialize();

And use fragment's default constructor. You may need to call setPlacePickerSettingsFromBundle() after you have set the arguments, something like this:

public PlacePickerFragment() {
super(GraphPlace.class, R.layout.com_facebook_placepickerfragment, args);
}

public void initialize() {
Bundle args = getArguments();
setPlacePickerSettingsFromBundle(args);
}


Related Topics



Leave a reply



Submit