[Fragment Series 7] Fragment switching optimization and fragment Switching

Source: Internet
Author: User

[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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.