android multiple fragments communicating to the activity through the same interface -
i working on application expect migrate multiple platforms. facilitate porting, i'm using fragments
streamline process. have right single activity
, several fragments
. in 1 case have several instances of same fragment
, different data. each of these instances of same fragment use same interface functions.
i've added parameter "tag" interface function identify each instance of fragment (using tag used in creating fragment). guess 1 use resource id
.
my question: way this? there better or more accepted method?
activity code snippet:
public class sailboatraceactivity extends fragmentactivity implements sr_commandsfragment.onsr_commandsselectedlistener, sr_subcommands_fragment.onsr_subcommandsselectedlistener, fleetlistfragment.fleetlist_widthchange, fleetlistfragment.fleetlistchange, fleetscoringfragment.onfs_selectedlistener, fleetspinnerfragment.fleetspinner_selection { // state variables private int msubcommandpositon; private int mcommandposition; private int mfleetselection; private int mfleetlistcolumnwidth = 0; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (savedinstancestate != null) { msubcommandpositon = savedinstancestate.getint("msubcommandpositon"); mcommandposition = savedinstancestate.getint("mcommandposition"); mfleetselection = savedinstancestate.getint("mfleetselection"); mfleetlistcolumnwidth = savedinstancestate.getint("mfleetlistcolumnwidth"); } setcontentview(r.layout.sailboat_race_layout); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); } public void onsr_commandselected(int position) { sr_subcommands_fragment sr_subcfrag = (sr_subcommands_fragment) getfragmentmanager().findfragmentbyid(r.id.subcommands); mcommandposition = position; if (sr_subcfrag != null) { sr_subcfrag.update_sr_subcommands(position); sr_subcfrag.changelistviewwidth(mfleetlistcolumnwidth); } } public void onsr_subcommandselected(int position) { msubcommandpositon = position; fleetscoringfragment fsfrag = (fleetscoringfragment) getfragmentmanager().findfragmentbyid(r.id.fleet_scoring); if (fsfrag != null) { fsfrag.update_commands(mcommandposition, msubcommandpositon); } } @override public void onsaveinstancestate(bundle outstate) { outstate.putint("msubcommandpositon", msubcommandpositon); outstate.putint("mcommandposition", mcommandposition); outstate.putint("mfleetselection", mfleetselection); outstate.putint("mfleetlistcolumnwidth", mfleetlistcolumnwidth); super.onsaveinstancestate(outstate); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void fleetlist_widthchange(int width) { mfleetlistcolumnwidth = width; // can set width of other 2 lists sr_subcommands_fragment sr_subcfrag = (sr_subcommands_fragment) getfragmentmanager().findfragmentbyid(r.id.subcommands); if (sr_subcfrag != null) { sr_subcfrag.changelistviewwidth(width); } sr_commandsfragment sr_cfrag = (sr_commandsfragment) getfragmentmanager().findfragmentbyid(r.id.commands); if (sr_cfrag != null) { sr_cfrag.changelistviewwidth(width); } } @override public void fleetlistchange(int position) { fleetscoringfragment fsfrag = (fleetscoringfragment) getfragmentmanager().findfragmentbyid(r.id.fleet_scoring); if (fsfrag != null) { mfleetselection = position; fsfrag.update_fleetscoringlist(position); } fleetscoringheaderfragment fshfrag = (fleetscoringheaderfragment) getfragmentmanager().findfragmentbyid(r.id.scoring_header); if (fshfrag != null) { mfleetselection = position; fshfrag.update_fleetscoringheader(position); } } @override public void onfs_selected(int position) { } @override public void fspinner_selection(int selection, string tag) { int fs = selection; string thistag =tag; // }
the fspinner_selection interface 1 i'm working on. set implementation check if data valid.
fleetspinner snippet:
public class fleetspinnerfragment extends fragment { private int npresentfleetposition; public arraylist<fleetitem> mlistarray = new arraylist<fleetitem>(); fleetspinneradapter adapter; fleetspinner_selection mcallback; spinner spinner; string tag; public interface fleetspinner_selection { public void fspinner_selection(int selection, string tag); } public fleetspinnerfragment() { } @override public void onattach(activity activity) { super.onattach(activity); try { mcallback = (fleetspinner_selection) activity; } catch (classcastexception e) { throw new classcastexception(activity.tostring() + " must implement fspinner_selection"); } } @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); if (savedinstancestate == null) { npresentfleetposition = 0; (int = 0; i<fleet.fleetbit.length;i++) { mlistarray.add(fleet.fleetbit[i]); } } else { npresentfleetposition = savedinstancestate.getint("npresentfleetposition"); arraylist temp = savedinstancestate.getparcelablearraylist("thisfleetlist"); mlistarray.addall(temp); } } @override public void onsaveinstancestate(bundle savedinstancestate) { savedinstancestate.putint("npresentfleetposition", npresentfleetposition); arraylist<fleetitem> temp = new arraylist<fleetitem>(); (int =0 ; i< adapter.getcount();i++){ temp.add(adapter.getitem(i)); } savedinstancestate.putparcelablearraylist("thisfleetlist", temp); super.onsaveinstancestate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = new view(getactivity()); adapter = new fleetspinneradapter(getactivity(), r.layout.fleet_list_item); spinner = new spinner(getactivity()); spinner.setadapter(adapter); adapter.addall(fleet.fleetbit); spinner.setonitemselectedlistener(new customonitemselectedlistener()); tag = gettag(); spinner.setselection(npresentfleetposition); return spinner; } public void update_fleetscoringheader(int position) { npresentfleetposition = position; framelayout flscoringheader = (framelayout) getactivity().findviewbyid(r.id.scoring_header); textview v1 = (textview) flscoringheader.findviewbyid(r.id.fleet_list_item_textview); v1.settext(fleet.fleetbit[npresentfleetposition].name); imageview v0 = (imageview) flscoringheader.findviewbyid(r.id.fleet_list_item_icon); v0.setimageresource(fleet.fleetbit[npresentfleetposition].id); } static class fleetholder { imageview imgicon; textview txttitle; } public class customonitemselectedlistener extends activity implements adapterview.onitemselectedlistener { @override public void onitemselected(adapterview<?> parent, view view, int position, long id) { fleetitem temp = (fleetitem) parent.getitematposition(position); npresentfleetposition = position; mcallback.fspinner_selection(position, tag); } @override public void onnothingselected(adapterview<?> parent) { } }
i'm starting learn android programming, may noob question.
Comments
Post a Comment