http://blog.csdn.net/leelit/article/details/38776931
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
public class Mainactivity extends Actionbaractivity implements Onclicklistener {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; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main);//Initialize 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 Iamgeviewweixiniv = (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 TEXTVIEWWEIXINTV = (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 layoutweixinlayout = (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);//Layout Registration Listener Weixinlayout.setonclicklistener (this); Tongxunlulayout.setonclicklistener (This), Faxianlayout.setonclicklistener (this); Wolayout.setonclicklistener ( this);} @Overridepublic void OnClick (View arg0) {//TODO auto-generated method stub//When you click on a layout, the state is cleared first, The state here refers to the layout of the picture and text clearstate (), switch (Arg0.getid ()) {case r.id.weixin_layout://If the point is, Changes 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 fragmentshowfragment (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;}} public void Clearstate () {//unselected picture Weixiniv.setbackgroundresource (r.drawable.weixin1); Tongxunluiv.setbackgroundresource (R.DRAWABLE.TONGXUNLU1); Faxianiv.setbackgroundresource (R.DRAWABLE.FAXIAN1); Woiv.setbackgroundresource (R.DRAWABLE.WO1);//The Font Color Weixintv.settextcolor (getresources () when unchecked. 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));} public void showfragment (int index) {fragmenttransaction ft = fm.begintransaction ();//To show a fragment, hide all Frag firstment, prevent overlapping hidefragments (ft); switch (index) {case 1://if fragment1 already exists then display it out if (fragment1! = null) ft.show (FRAGMENT1); /Otherwise is the first time to switch to add fragment1, note that after the addition is displayed, replace method is also first remove after Addelse {fragment1 = new Fragment1 (); Ft.add (R.id.content, FRAGMENT1);} Break;case 2:if (Fragment2! = null) ft.show (FRAGMENT2); else {fragment2 = new Fragment2 (); Ft.add (R.id.content, Fragment2) ;} Break;case 3:if (Fragment3! = null) ft.show (FRAGMENT3); else {fragment3 = new Fragment3 (); Ft.add (R.id.content, Fragment3) ;} Break;case 4:if (fragment4! = null) ft.show (FRAGMENT4); else {fragment4 = new Fragment4 (); Ft.add (R.id.content, FRAGMENT4) ;} break;} Ft.commit ();} When fragment has been instantiated, it hides the public void hidefragments (fragmenttransaction ft) {if (fragment1! = null) ft.hide (FRAGMENT1); Fragment2 = null) ft.hide (FRAGMENT2), if (fragment3! = null) ft.hide (FRAGMENT3); if (fragment4! = null) ft.hide (FRAGMENT4) ;}}
When we started to pull fragment's ListView down to like, switch to address Book fragment, and then switch back to fragment, the ListView is still the original state, this is because not reload fragment, but will its first hide up , switch back and show it again.
Refer to the Fragment life cycle diagram of the previous article:
In the case of the Repalce method, we switch to the current fragment for the life cycle above the red line, and the life cycle below the red line after switching to the other fragment. However, if we use the Hide () and show () methods, switching to the current fragment still carries the life cycle above the red line, and after switching to the other fragment does not carry on the other life cycle, but simply hides up. That should be clear.
Source:
http://download.csdn.net/detail/leelit/8179147
Android components: Fragment save status after switching