java - Why instance a new fragment inside a public static class -
reference: fragment documentation
i new java , android , going through online android guide. have 2 questions:
question 1: why there need create new instance of detailsfragment within public static class?
question 2: "create new instance" farther declares new detailsfragment. overall there 4 places same name "detailsfragment" used. confusing. please explain?
public static class detailsfragment extends fragment { /* create new instance of detailsfragment, initialized show text @ 'index'. */ public static detailsfragment newinstance(int index) { detailsfragment f = new detailsfragment(); // supply index input argument. bundle args = new bundle(); args.putint("index", index); f.setarguments(args); homecoming f; } }
that's called static mill method pattern, , in case allows create new instance of fragment of required arguments populated, without having know internal implementation. example, without this, create initialized instance of fragment, use:
detailsfragment fragment = new detailsfragment(); bundle args = new bundle(); args.putint("index", index); fragment.setarguments(args); // utilize fragment need using static mill method, of logic internalized fragment itself, , can use:
detailsfragment fragment = detailsfragment.newinstance(index); // utilize fragment java android android-fragments
No comments:
Post a Comment