android fragment - data passing - best practice -
this question has answer here:
when passing data fragment why - (i.e. way supposed it)
private string mparam1; private string mparam2; public static reviewpagesfragment newinstance(string param1, string param2) { reviewpagesfragment fragment = new reviewpagesfragment(); bundle args = new bundle(); args.putstring(arg_param1, param1); args.putstring(arg_param2, param2); fragment.setarguments(args); return fragment; } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (getarguments() != null) { mparam1 = getarguments().getstring(arg_param1); mparam2 = getarguments().getstring(arg_param2); } }
instead of doing
private string mparam1; private string mparam2; public static reviewpagesfragment newinstance(string param1, string param2) { reviewpagesfragment fragment = new reviewpagesfragment(); fragment.mparam1 = param1; fragment.mparam2 = param2; return fragment; }
the second way simpler , gives better performance. fan of k.i.s.s. question - why first way recommended way , benefit come doing way?
using bundle allows fragment persist state if it's recreated. here's more thorough explaination: best practice instantiating new android fragment
Comments
Post a Comment