android - Proper Replacement of Fragments -


i have 1 activity called a. activity has 1 frame layout in fragments used. have 2 fragments, fragment1 , fragment2. when activity launched, fragment 1 fills frame layout.

fragment1 contains button when clicked replaces fragment2 within same frame layout. question this, when click button in fragment1 should implement code

a) activity gets notified of onclick in fragment through interface using type of boolean value , proceeds replace fragment2.

or

b)implement code replaces fragment1 fragment2 within fragment1 example:

private fragmenttransaction ft; private button registerbutton, resetbutton; private fragment fragment;  public loginfragment() {     // required empty public constructor }   @override public view oncreateview(layoutinflater inflater, viewgroup container,                          bundle savedinstancestate) {     view view = inflater.inflate(r.layout.fragment_login, container, false);      registerbutton = (button)view.findviewbyid(r.id.register_button);     resetbutton = (button) view.findviewbyid(r.id.reset_button);     registerbutton.setonclicklistener(this);     resetbutton.setonclicklistener(this);      return view; }  @override public void onclick(view v) {     switch (v.getid()) {         case r.id.register_button: {             fragment = new registerfragment();             ft = getfragmentmanager().begintransaction();             ft.replace(r.id.content_frame, fragment);             ft.addtobackstack(null);             ft.settransition(fragmenttransaction.transit_fragment_open);             ft.commit();             break;         }     } } 

could explain why 1 on other? much!

generally, use interface of sort lives in fragment being replaced (in case fragment 1). parent activity implement interface, , building contract between activities parent of particular fragment.

when press button (or whatever event happens signal replace), grab activity casting interface, , call particular method.

e.g. signaling event within fragment

( (myfragmentlistener) getactivity()).onactionhappens(); 

where myfragmentlistener inner class of fragment , onactionhappens() method sends signal. creates contract between fragment , activity hosts fragment. when action happens, let activity know , activity overrides appropriate method handle event.

there other ways this, @ simplest level how can done.

why not option b

option b creates tight coupling between fragments, don't want. in practice want coupling between fragment, , it's host (or parent) activity. also, there many activities use fragment abstract away details particular activity uses calling getactivity(). in case, coupling fragment , activity acceptable, since of course 2 coupled anyways. know because fragment cannot live without associated activity, okay take advantage of tight coupling.

summary

pick option a. cleanest route, , avoids assuming implementation details have in option b.

it basic solution have without external libraries or details required. if want more advanced solution, checkout otto (made square) link library here


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

android - Keyboard hides my half of edit-text and button below it even in scroll view -