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. if (fragment1! =Null Ft.show (FRAGMENT1); Otherwise it is the first time to switch to add fragment1, note that after the addition is displayed, the Replace method is also first remove after the add else { Fragment1 =New Fragment1 (); Ft.add (R.id.content, fragment1); } Break Case2: if (fragment2! =Null Ft.show (Fragment2); else { Fragment2 =New Fragment2 (); Ft.add (R.id.content, Fragment2); } Break Case3: if (fragment3! =Null Ft.show (FRAGMENT3); else { Fragment3 =New Fragment3 (); Ft.add (R.id.content, FRAGMENT3); } Break Case4: 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 is hidden. Publicvoid Hidefragments (fragmenttransaction ft) { if (fragment1! = null) Ft.hide (fragment1); if (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 state after switching