1

I am working on an app that uses this example as a base. Scroll down to the class called "DetailsFragment". You will see this method:

public static DetailsFragment newInstance(int index) {
    DetailsFragment f = new DetailsFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();
    args.putInt("index", index);
    f.setArguments(args);

    return f;
}

Why is this method static ? Couldn't this be done like a regular constructor like this:

public DetailsFragment(int index) {
    Bundle args = new Bundle();
    args.putInt("index", index);
    this.setArguments(args);
}

And then when you need the object just go:

DetailsFragment f = new DetailsFragment(somevalue);

I don't see why this method is static.

  • related: http://stackoverflow.com/questions/929021/what-are-static-factory-methods-in-java – zapl Aug 16 '13 at 09:15

4 Answers4

4

Why is this method static ? Couldn't this be done like a regular constructor like this

Basically the first approach is using static factory method. In this case, there might be no difference. You can write same code in a constructor. Well, there would actually be an Android specific issue, as specified in comments by @zapl. If you provide your own parameterized constructor, then the compiler won't provide a default constructor. As specified in comments, every Fragment must have a default constructor.

But, in general, there are several benefits of using static factory method. Some of them are:

  • You can implement singleton pattern with static factory method
  • A static factory method can return you an instance of any subclass.

The best reference regarding this topic you can find in Effective Java book - Item 1, that I've linked below.

Reference:

Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • 1
    There is also an Android specific reason: Every `Fragment` must have a no argument public constructor so the system can (re-)create them. – zapl Aug 16 '13 at 09:19
  • 1
    @zapl. Oh! Yeah that is also a point. Compiler won't create a default constructor, once we provide a parameterized one explicitly. – Rohit Jain Aug 16 '13 at 09:20
  • Ah - thanks both Rohit and zapl. I am aware of some of the uses of statics, but I didn't realize it was because of what zapl said in this specific case. I am working on expanding the DetailsFragment in this example into three different fragments that can be plugged into respective ActionBarSherlock tabs. – CupOfQuestions Aug 16 '13 at 09:37
1

Both approaches are possible in this scenario and equally good.

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
  • Sorry, I edited removed the "return this", before you hit submit :) (for those seeing this, i had a return this in the regular constructor above) – CupOfQuestions Aug 16 '13 at 11:31
0

I am not sure why we are creating object of Bundle when creating instance of DetailsFragment. we can separate both in DetailsFragment class.

0

You can use DetailsFragment(int index) and it could be called only by you. In your case it is no problem, because you use setArguments instead of class-vars.

Why it is a pattern to use newInstance?

If your OS kill your Fragment, and next time your Fragment will be restored, only the Default-Constructor will be called. The DetailsFragment(int index) won't be called again by the OS. However the arguments you set, could be ever stored in memory although your Fragment has been killed once.

TeeTracker
  • 7,064
  • 8
  • 40
  • 46