android - Two dynamic fragment on screen (back) -
i have application fragment. start 2 dynamic fragments on screen. on fragment b, have list. when click on item, change 2 fragments c , d.
_____ _____ _____ _____ | | | c | | | | | _____ => _____ when back, have : _____ _____ | b | | d | | d | | b | _____ _____ _____ _____
when press button back, have top fragment change, fragment d stay visible whereas want see a/b.
this class :
public class mainactivity extends activity implements fragmentblistener, fragmentclistener { private static final string tag_fragment_a = "fragmentatag"; private static final string tag_fragment_b = "fragmentbtag"; private static final string tag_fragment_c = "fragmentctag"; private static final string tag_fragment_d = "fragmentdtag"; fragmentmanager fragmentmanager = getfragmentmanager(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override protected void onrestart() { super.onrestart(); } @override protected void onstart() { super.onstart(); initfragments(); } private void initfragments() { setfragment(tag_fragment_a, r.id.top_framelayout); setfragment(tag_fragment_b, r.id.main_framelayout); } private fragment setfragment( string tagfragment, int containerviewid) { fragmenttransaction ftransaction = fragmentmanager.begintransaction(); fragment mfragment = null; if(tagfragment.equals(tag_fragment_a)) mfragment = setfragmenta(ftransaction, containerviewid); else if(tagfragment.equals(tag_fragment_b)) mfragment = setfragmentb(ftransaction, containerviewid); else if(tagfragment.equals(tag_fragment_c)) mfragment = setfragmentc(ftransaction, containerviewid); else if(tagfragment.equals(tag_fragment_d)) mfragment = setfragmentd(ftransaction, containerviewid); ftransaction.addtobackstack(null); ftransaction.commit(); return mfragment; } private fragment setfragmenta(fragmenttransaction ftransaction, int containerviewid) { fragmenta fragment = (fragmenta) fragmentmanager.findfragmentbytag(tag_fragmenta); if (fragment == null) { fragment = new fragmenta(); if(fragment != null) ftransaction.replace(containerviewid, fragment,tag_fragmenta); } return fragment; } private fragment setfragmentb(fragmenttransaction ftransaction, int containerviewid) { fragmentb fragment = (fragmentb) fragmentmanager.findfragmentbytag(tag_fragment_b); if (fragment == null) { fragment = new fragmentb(); if(fragment != null) ftransaction.replace(containerviewid, fragment,tag_fragment_b); } return fragment; } private fragment setfragmentc(fragmenttransaction ftransaction, int containerviewid) { fragmentc fragment = (fragmentc) fragmentmanager.findfragmentbytag(tag_fragment_c); if (fragment == null) { fragment = new fragmentc(); if(fragment != null) ftransaction.replace(containerviewid, fragment,tag_fragment_c); } return fragment; } private fragment setfragmentd(fragmenttransaction ftransaction, int containerviewid) { fragmentd fragment = (fragmentd) fragmentmanager.findfragmentbytag(tag_fragment_d); if (fragment == null) { fragment = new fragmentd(); if(fragment != null) ftransaction.replace(containerviewid, fragment,tag_fragment_d); } return fragment; } @override public void onupdatefragmentb() { fragmentb fragmentb = (fragmentb) fragmentmanager.findfragmentbytag(tag_fragment_b); if (fragmentb != null) { fragmentb.setdata(...); } } @override public void onupdated() { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentd fragment = (fragmentd) fragmentmanager.findfragmentbytag(tag_fragment_d); fragment.setdata(); } @override public void changefragments() { fragmentd fragmentd = (fragmentd) setfragment(tag_fragment_d, r.id.main_framelayout); if(fragmentd != null) fragmentd.setdata(...); fragmentc fragmentc = (fragmentc) setfragment(tag_fragment_c, r.id.top_framelayout); if(fragmentc != null) fragmentc.setdata(...); } @override public void onupdatefragmentsab { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentb fragmentb =(fragmentb) fragmentmanager.findfragmentbytag(tag_fragment_b); if(fragmentb != null) fragmentb.setdata(...); fragmenta fragmenta = (fragmenta) fragmentmanager.findfragmentbytag(tag_fragment_a); if(fragmenta != null) fragmenta.setdata(...); } }
you need replace both fragments @ time come previous one, .
public void setfragment(fragment topfrag, fragment bottomfrag){ fragmentmanager fragmentmanager=getfragmentmanager(); fragmenttransaction transaction=fragmentmanager.begintransaction(); transaction.replace(r.id.frag_top, topfrag); transaction.replace(r.id.frag_bottom, bottomfrag); transaction.addtobackstack(null); transaction.commit(); }
Comments
Post a Comment