Office Hours — Today, April 7

Yesterday, April 6

Apr 7
3:55 PM
Mark M.
has entered the room
Mark M.
turned on guest access
Steve S.
has entered the room
Mark M.
hello, Steve!
how can I help you today?
Steve S.
Hi Mark!
I have a question regarding passing parameters to fragments. I'll upload it.
View paste
I have a question regarding the pattern for passing arguments to a fragment of using a static factory method. As I understand it, if you call Fragment#setArguments() in the factory method, the arguments will be preserved across rotations. 

I'm wondering why the pattern is to use a static factory method rather than a constructor with arguments. I realize that there needs to be a constructor with no arguments available to the framework to recreate the fragment. But why not have two constructors, one with arguments (in which you call Fragment#setArguments() )  and one without, instead of a constructor with no arguments and the static factory method? Is there something special about the factory method or is this simply a convention?
4:00 PM
Mark M.
it's simply a convention AFAIK
personally, I like it, as it means that you simply have no constructors
and that makes it an easy pattern to follow and validate
Steve S.
Great. That's very helpful. It was hard to figure out the actual situation from Stack Overflow, etc.
I want to create a builder for a fragment.
So it sounds like I could move what I now have in newInstance() into the build() method of the builder.
Mark M.
correct
Steve S.
Great. No more questions today.
Mark M.
usually, fragments aren't complex enough to warrant a builder, which is why you don't see that much
but, given a sufficiently-complex fragment, a builder seems like a perfectly reasonable approach
Steve S.
Sure. This is for a DialogFragment with a number of parameters that need to be set for the UI.
Thanks so much for your help. Have a great day.
Mark M.
you too!
Steve S.
has left the room
4:55 PM
Mark M.
turned off guest access

Yesterday, April 6

 

Office Hours

People in this transcript

  • Mark Murphy
  • Steve S.