java - Extending SupportMapFragment - how to instantiate new fragment? -


in documentation mapfragment , supportmapfragment create new fragment calling newinstance() instead of using new supportmapfragment().

my app project extends supportmapfragment, , tried call mymapfragment.newinstance() on fragment class, resulting in map showing expected none of overridden methods such oncreateview() , onactivitycreated() being called. took me while before tried instantiating fragment using new mymapfragment() instead - , voĆ­la, overridden methods started getting called!

i didn't override newinstance() in class, , in hindsight it's obvious newinstance() returns instance of supportmapfragment, not instance of extended class (duh!).

but question - why is there newinstance() method , why documentation use it, when seems work using new supportmapfragment()? what's difference of using 1 or other? haven't been able find source code supportmapfragment, so...

in case believe newinstance method static factory method empty constructor has no effect (though without source code available cannot know sure), i.e. like:

public static supportmapfragment newinstance() {     return new supportmapfragment(); } 

so why exist?

  • for consistency other newinstance(googlemap) method likely
  • in theory method return sub-class of supportmapfragment, perhaps 1 optimised device or platform
  • in case arguments need set (perhaps or in future)

because of last point, practice use static factories when creating fragments, in future modified to:

public static supportmapfragment newinstance() {     supportmapfragment fragment = new supportmapfragment();     bundle args = new bundle();     args.putboolean("secretoptionnotenabledwithnormalconstructor", true);     fragment.setarguments(args);     return fragment; } 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -