java - Android Fragment instantiation: newInstance() -
i read many articles , stackoverflow answers still cannot understand why need factory method create instance of fragment.
the following fragment classes both work fine.
fragment 2 constructors:
public class ctorfragment extends fragment { private static final string key = "the_key"; public ctorfragment() { // android calls default constructor default constructor must explicitly defined. // have constructor, android won't create default constructor us. } public ctorfragment(string s) { // set arguments. bundle bundle = new bundle(); bundle.putstring(key, s); setarguments(bundle); } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) { view fragment = inflater.inflate(r.layout.fragment_my, container, false); textview textview = (textview) fragment.findviewbyid(r.id.textview); // use getarguments() string argument set constructor parameter. textview.settext(getarguments().getstring(key)); return fragment; } }
fragment static factory method:
public class staticfragment extends fragment { private static final string key = "the_key"; public static staticfragment newinstance(string s) { staticfragment fragment = new staticfragment(); // set arguments. bundle bundle = new bundle(); bundle.putstring(key, s); fragment.setarguments(bundle); return fragment; } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, bundle savedinstancestate) { view fragment = inflater.inflate(r.layout.fragment_my, container, false); textview textview = (textview) fragment.findviewbyid(r.id.textview); // use getarguments() string argument set constructor parameter. textview.settext(getarguments().getstring(key)); return fragment; } }
can please explain why (including google) "strongly recommends" 1 static factory method? there critical me , others coming non-android background missing?
is have define 2 methods (constructors) instead of 1 (static factory method) causes fuss?
still cannot understand why need factory method create instance of fragment
"need" strong word. not "need" factory method. do need:
a public zero-argument constructor, ideally empty; and
an organized means set fragment, in way survives configuration changes
can please explain why (including google) "strongly recommends" 1 static factory method?
if have no explicit constructors on java class, automatically empty public zero-argument constructor, framework needs create fragment.
if create constructor takes arguments (e.g., ctorfragment(string s)
), have remember create public zero-argument constructor (ctorfragment()
). might remember this. many inexperienced programmers not.
writing factory method achieves same objective non-zero-arguments constructor, without clobbering automatically-created zero-argument constructor.
if experienced java programmer, , not factory methods, , can read stack traces , otherwise remember add public zero-argument constructor, welcome create additional constructors , use them.
wiki
Comments
Post a Comment