I have always created fragments and passed my parameters to the fragment by using the newInstance() pattern. This works great for one or two parameters. I'm creating a fragment now that has around 10 parameters, and all of them are optional.
I was thinking about using the Builder pattern, similar to how the AlertDialog works. However, I'm not exactly sure what would be the best way to implement this, or if it is even a good idea.
Here is an example of what I was thinking, but with many more variables.
public class MyFragment extends Fragment {
private String name;
private static class Builder {
private String name;
public Builder setName(String name) {
this.name = name;
return this;
}
public MyFragment build() {
return new MyFragment(this);
}
}
// NOT ALLOWED
public MyFragment(Builder builder) {
name = builder.name;
}
// Rest of the fragment...
}
The problem with this is that the fragment must have a default constructor, so this will not work.
Is there a "correct" way to accomplish this?