The first fragment instance written earlier, as most people start with, is implemented by the Fragmenttransaction replace method, which is equivalent to removing remove () from all existing fragments, Then add () the current fragment. This leads to a problem, we switch once, and then switch back, the equivalent of reloading the fragment, the original state no longer exist, which is obviously inconsistent with our daily use. Want to save the state after the switch, the idea is still very simple, we first added a number of fragments, after switching all fragments hides hide (), and Show () after the switch fragment can be.
Example: Cottage
Because the code is long, here is only the core part, interested can download the source to see
PublicClassMainactivityExtendsActionbaractivityImplementsOnclicklistener { Private View Weixinlayout, Tongxunlulayout, Faxianlayout, wolayout; Private TextView Weixintv, TONGXUNLUTV, Faxiantv, Wotv; Private ImageView Weixiniv, Tongxunluiv, Faxianiv, WOIV; Private Fragment1 fragment1; Private Fragment2 Fragment2; Private Fragment3 Fragment3; Private Fragment4 fragment4; Private Fragmentmanager FM; @Override Protectedvoid OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Initialization Initviews (); fm = Getfragmentmanager (); The first knowledge state is the display Weixiniv.setbackgroundresource (r.drawable.weixin2); Weixintv.settextcolor (Getresources (). GetColor (R.color.green)); Showfragment (1);} void Initviews () { Register each Iamgeview Weixiniv = (ImageView) Findviewbyid (R.ID.WEIXIN_IV); Tongxunluiv = (ImageView) Findviewbyid (R.ID.TONGXUNLU_IV); Faxianiv = (ImageView) Findviewbyid (R.ID.FAXIAN_IV); WOIV = (ImageView) Findviewbyid (R.ID.WO_IV); Register each TextView Weixintv = (TextView) Findviewbyid (R.ID.WEIXIN_TV); TONGXUNLUTV = (TextView) Findviewbyid (R.ID.TONGXUNLU_TV); Faxiantv = (TextView) Findviewbyid (R.ID.FAXIAN_TV); Wotv = (TextView) Findviewbyid (R.ID.WO_TV); Register each layout Weixinlayout = (View) Findviewbyid (r.id.weixin_layout); Tongxunlulayout = (View) Findviewbyid (r.id.tongxunlu_layout); Faxianlayout = (View) Findviewbyid (r.id.faxian_layout); Wolayout = (View) Findviewbyid (r.id.wo_layout); Each layout registration listener Weixinlayout.setonclicklistener (this); Tongxunlulayout.setonclicklistener (this); Faxianlayout.setonclicklistener (this); Wolayout.setonclicklistener (this);} @Override Publicvoid OnClick (View arg0) { TODO auto-generated Method Stub When you click on a layout, clear the state, where the state refers to the layout of the picture and text Clearstate (); Switch (Arg0.getid ()) { Case R.id.weixin_layout: If the point is, change the color of the layout's picture and text to green Weixiniv.setbackgroundresource (r.drawable.weixin2); Weixintv.settextcolor (Getresources (). GetColor (R.color.green)); Display of Fragment Showfragment (1); Break Case R.id.tongxunlu_layout: Tongxunluiv.setbackgroundresource (R.DRAWABLE.TONGXUNLU2); Tongxunlutv.settextcolor (Getresources (). GetColor (R.color.green)); Showfragment (2); Break Case R.id.faxian_layout: Faxianiv.setbackgroundresource (R.DRAWABLE.FAXIAN2); Faxiantv.settextcolor (Getresources (). GetColor (R.color.green)); Showfragment (3); Break Case R.id.wo_layout: Woiv.setbackgroundresource (R.DRAWABLE.WO2); Wotv.settextcolor (Getresources (). GetColor (R.color.green)); Showfragment (4); Break }} Publicvoid Clearstate () { Picture When not selected Weixiniv.setbackgroundresource (r.drawable.weixin1); Tongxunluiv.setbackgroundresource (R.DRAWABLE.TONGXUNLU1); Faxianiv.setbackgroundresource (R.DRAWABLE.FAXIAN1); Woiv.setbackgroundresource (R.DRAWABLE.WO1); Font color when not selected Weixintv.settextcolor (Getresources (). GetColor (R.color.black)); Tongxunlutv.settextcolor (Getresources (). GetColor (R.color.black)); Faxiantv.settextcolor (Getresources (). GetColor (R.color.black)); Wotv.settextcolor (Getresources (). GetColor (R.color.black));} Publicvoid Showfragment (int index) { Fragmenttransaction ft = fm.begintransaction (); Want to show a fragment, hide all fragment first, prevent overlapping Hidefragments (FT); Switch (index) { Case1: If the fragment1 already exists, it will be displayed.