Android Fragment initialization to various Custom Fragments, but only One at a time -


i trying initialize generic fragment in such manner can used globally no matter instance of is. example:

 public mainactivity extends activity{     private fragment fragment;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_first_layout);         fragmentmanager fragmanager = getfragmentmanager();         fragmenttransaction transaction = fragmanager.begintransaction();          if(condition1){             fragment =  new myfirstfragment();             transaction             .add(r.id.registrationfragment, fragment); //registrationfragment framelayout             transaction.commit();          }else if(condition2){             fragment = new mysecondfragment();             transaction             .add(r.id.registrationfragment, fragment); //registrationfragment framelayout             transaction.commit();         }         //create intent filter         //create mybroadcast object         //registerreceiver(receiverobject, intentfilter)             }      //some action happens      class mybroadcast extends broadcastreceiver {      public void onreceive(context context, intent intent) {          string action = intent.getaction();         if (action.equals(action1)) {             fragment.performaction();         }        }   }  public class myfirstfragment extends fragment {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view view = inflater.inflate(                 r.layout.fragment_holder_a, container, false);         //setwidgets etc...         // todo auto-generated method stub         return view;     }      //used main     public void performaction(string text){         //set textview's text mainactivity event         //different mysecondfragment performaction method     }  }  public class mysecondfragmentfragment extends fragment {     @override     public view oncreateview(layoutinflater inflater, viewgroup container,             bundle savedinstancestate) {         view view = inflater.inflate(                 r.layout.fragment_holder_b, container, false);         //setwidgets etc...         // todo auto-generated method stub         return view;     }      //used main     public void performaction(string text){         //set textview's text mainactivity event         //different myfirstfragment performaction method     } } 

the problem in real application may have more 2 fragments fragment object become instance of. trying avoid lot of if statements check fragment instance inflated fragmentmanager. problem facing when declare fragment fragment = new myfirstfragment(); cannot access fragment.performaction("stringssss") method. not solution looking for:

public mainactivity extends activity{     private myfirstfragment myfirstfrag;     private mysecondfragment mysecondfrag;     private mythirdfragment mythirdfrag;     private mynthfragment mynthfrag;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_first_layout);         fragmentmanager fragmanager = getfragmentmanager();         fragmenttransaction transaction = fragmanager.begintransaction();          if(condition1){             myfirstfrag =  new myfirstfragment();             transaction             .add(r.id.registrationfragment, myfirstfrag); //registrationfragment framelayout             transaction.commit();          }else if(condition2){             mysecondfrag = new mysecondfragment();             transaction             .add(r.id.registrationfragment, mysecondfrag); //registrationfragment framelayout             transaction.commit();         }else if(condition3){             //...         }else if(conditionnth){             //...         }         //create intent filter         //create mybroadcast object         //registerreceiver(receiverobject, intentfilter)             }      //some action happens      class mybroadcast extends broadcastreceiver {      public void onreceive(context context, intent intent) {          string action = intent.getaction();         if (action.equals(action1)) {             if(null != myfirstfrag)                 myfirstfrag.performaction();             }else if(null != mysecondfrag){                 mysecondfrag.performaction();             }else if(null != mythirdfrag){                 mythirdfrag.performaction();             }else if(null != mynthfrag){                 mynthfrag.performaction();             }         }else if(action.equals(actionnth)){             if(null != myfirstfrag)                 myfirstfrag.performaction();             }else if(null != mysecondfrag){                 mysecondfrag.performaction();             }else if(null != mythirdfrag){                 mythirdfrag.performaction();             }else if(null != mynthfrag){                 mynthfrag.performaction();             }         }        }   } 

as can see in broadcastreceiver lot of if-else statements start pile making code clunky , dirty. looking clean solution not require me have bunch of if-else statements , keep code simple.

thank you.


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -