[Fragment Series 7] Fragment switching optimization and fragment Switching
1. Fragment + RadioGroup
Failover failover requires Fragment switching in the project. It always uses the replace () method to replace Fragment. Then, it always feels stuck during the switchover.
1. Previous Code:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkID) { switch (checkID) { case R.id.rabt01: if (searchFragment == null) { searchFragment = new SearchFragment(); getSupportFragmentManager().beginTransaction().hide().add(R.id.linnerlayout, searchFragment).commit(); } break; case R.id.rabt02: downFragment = new DownFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.linnerlayout, downFragment).commit(); break; case R.id.rabt03: listenFragment = new ListenFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.linnerlayout, listenFragment).commit(); break; case R.id.rabt04: myFragment = new MyFragment(); getSupportFragmentManager().beginTransaction().replace(R.id.linnerlayout, myFragment).commit(); break; } } });
2. Cause Analysis:
Because replace calls the onCreateView () method of fragment again every time, it is a waste of time.
3. solution:
The correct switching method for zookeeper is add (). When switching, hide (), add () and another Fragment; when switching again, only the current hide () and show () are needed. In this way, the onCreateView function will not be called repeatedly.
Zookeeper FragmentTransactioin can be used to add or delete fragment. It can also control the display and hiding of fragment.
Zookeeper: If addToBackStack (null) is called, The status will be saved in the rollback stack. When you press the return key, the top of the stack will be displayed.
Android optimized Fragment prevent page multiple times inflate: http://www.th7.cn/Program/Android/201411/311607.shtml
4. Code Improvement
The method of switching between different Fragment: http://www.th7.cn/Program/Android/201503/408512.shtml
A global variable must be maintained to record the current fragment. In addition, some judgment needs to be made, which can be extracted as a method.
Optimization of Fragment switching: http://www.tuicool.com/articles/iEfIvqb
Ii. ViewPager + Fragment
Fragment trap: http://mobile.51cto.com/abased-446691.htm
Optimization of fragment + viewpager: http://www.bubuko.com/infodetail-648897.html