Android save fragment switching state instance code
preface
In general, frequent switching of fragments will lead to frequent release and creation. If fragments are bulky, the experience is very bad. Here is a method.
text
1、 Application scenario
1. Do not use viewpager
2. If you cannot use replace to switch fragments, the fragments will be released (call ondestroyview)
2、 Realize
1、xml
Code Description:
It is very common to put 5 RadioButtons at the bottom and click to switch different fragments.
2、Activity
Set setoncheckedchangelister event for RadioButton. Other codes:
Code Description:
Instantiateitem finds a fragment from the fragmentmanager. If it cannot be found, create a new getitem, set setprimaryitem to hide and display, and finally finish update submits the transaction. Mcontainer is the FrameLayout in XML.
3、 Fragmentpageradapter core code
Fragmentpageradapter is a class that comes with the support package.
4、 Attention
Before, I simulated viewpager and used attach, setmenuvisibility and setuservisiblehint to control the display and hiding of fragments. Fragments often overlap, which is a headache. After changing this, no overlap has been found.
Conclusion: the above is an example of Android fragment, hoping to help students in need.