android - static Factory method setting private variable when instationating Fragment -


i've noticed it's possible following:

private int msomevalue; public static myfragment newinstance(int somevalue){     myfragment myfragment = new myfragment();     myfragment.msomevalue = somevalue;     myfragment.dosth();     return myfragment; } private void dosth(){     //do sth } 

my question follows- acceptable way e.g. set values or listeners or other initial statet of custom fragment instance? or should use setarguments in order pass values on creation time ?

from this page:

all subclasses of fragment must include public no-argument constructor. framework re-instantiate fragment class when needed, in particular during state restore, , needs able find constructor instantiate it. if no-argument constructor not available, runtime exception occur in cases during state restore.

this means system destroy instance , remake later (something can't control!). because of must stick following conventions.

the new fragment wizard in android studio creates example of 2 things. i'll past code in describe it.

public class blankfragment extends fragment {      private static final string arg_param1 = "param1";     private static final string arg_param2 = "param2";      private string mparam1;     private string mparam2;      private onfragmentinteractionlistener mlistener;      public static blankfragment newinstance(string param1, string param2) {         blankfragment fragment = new blankfragment();         bundle args = new bundle();         args.putstring(arg_param1, param1);         args.putstring(arg_param2, param2);         fragment.setarguments(args);         return fragment;     }      public blankfragment() {         // required empty public constructor     }      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         if (getarguments() != null) {             mparam1 = getarguments().getstring(arg_param1);             mparam2 = getarguments().getstring(arg_param2);         }     }      @override     public void onattach(context context) {         super.onattach(context);         try {             mlistener = (onfragmentinteractionlistener) context;         } catch (classcastexception e) {             throw new classcastexception(context.tostring()                     + " must implement onfragmentinteractionlistener");         }          mlistener.fragmentattached(this);     }      @override     public void ondetach() {         mlistener.fragmentdetached(this);         super.ondetach();         mlistener = null;     }     public interface onfragmentinteractionlistener {         public void fragmentattached(blankfragment fragment);         public void fragmentdetached(blankfragment fragment);     }  } 

firstly, create new instance of fragment, use static newinstance method, can retrieve these arguments @ point using getarguments() method. example shows how in oncreate() callback.

secondly, fragment requires hosting activity implements interface. example gives activity chance add listeners or whatever wants fragment it's attached, , dispose of them when fragment detached.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -