0

In the documentation for MapFragment and SupportMapFragment they create a new fragment by calling newInstance() instead of using new SupportMapFragment().

My app project extends SupportMapFragment, and I tried to call MyMapFragment.newInstance() on my fragment class, resulting in the map showing up as expected but none of my overridden methods such as onCreateView() and onActivityCreated() were being called. Took me a while before I tried instantiating my fragment by using new MyMapFragment() instead - and voíla, my overridden methods started getting called!

I didn't override newInstance() in my class, and in hindsight it's obvious that newInstance() returns an instance of SupportMapFragment, not an instance of my extended class (duh!).

But my question is - why is there a newInstance() method and why does the documentation use it, when it seems to work just as well using new SupportMapFragment()? What's the difference of using one or the other? I haven't been able to find the source code for SupportMapFragment, so...

Magnus
  • 17,157
  • 19
  • 104
  • 189
  • see: http://stackoverflow.com/questions/9245408/best-practice-for-instantiating-a-new-android-fragment – pedromss Feb 05 '14 at 23:28

1 Answers1

1

In this case I believe the newInstance method is just a static factory method for the empty constructor so has no effect (though without the source code available we cannot know for sure), i.e. it is probably something like:

public static SupportMapFragment newInstance() {
    return new SupportMapFragment();
}

So why does it exist?

  • For consistency with the other newInstance(GoogleMap) method most likely
  • In theory the method could return a sub-class of SupportMapFragment, perhaps one optimised for the device or platform
  • In case any arguments need to be set (perhaps now or in the future)

Because of the last point, it is generally a good practice to always use static factories when creating fragments, in the future it could be modified to:

public static SupportMapFragment newInstance() {
    SupportMapFragment fragment = new SupportMapFragment();
    Bundle args = new Bundle();
    args.putBoolean("secretOptionNotEnabledWithNormalConstructor", true);
    fragment.setArguments(args);
    return fragment;
}
Joseph Earl
  • 23,351
  • 11
  • 76
  • 89
  • So should one make use of the superclass' newInstance() when extending it? If there is indeed something special going on inside SupportMapFragment.newInstance(), wouldn't it be best to use that as the "constructor" for my subclass? How would I do that exactly? – Magnus Feb 05 '14 at 23:47
  • You can't make use of the newInstance() when subclassing it because static methods cannot be overridden (static methods are really like global functions, the class name just provides some scope) – Joseph Earl Feb 05 '14 at 23:50
  • Alright, had a feeling that was the case. (but it's 1 am here so I couldn't really wrap my head around it right now :) Thanks. – Magnus Feb 05 '14 at 23:52