Fragment knowledge summary, fragment knowledge

Source: Internet
Author: User

Fragment knowledge summary, fragment knowledge
1. Why is Fragment called the fifth component?

Fragment saves more memory than Activity, and its switching mode is more comfortable. It uses no less than four components and has its own lifecycle, and must be attached to Activity.

2. How to Create Fragment by Activity

Static creation:

 

Dynamic Creation

FragmentManager fm = getSupportFragmentManager();FragmentTransaction ft = fm.beginTransaction();ft.add(R.id.fl_content, new MyFragment());ft.commit();
3. Differences between FragmentPageAdapter and FragmentPageStateAdapter

FragmentPageAdapter separates Fragment every time a page is switched. It is suitable for Fragment with fewer pages to save some memory, which will not affect the system memory much.

FragmentPageStateAdapter recycles the Fragment each time it switches the page. It is suitable for Fragment with more pages, so that it will not consume more memory.

4. Fragment Lifecycle onAttach() onCreate() onCreateView() onActivityCreated() onStart() onResume() onPause() onStop() onDestroyView() onDestroy() onDetach() 5. Fragment Communication Fragment calls the method in Activity: getActivity calls the method in Fragment: calls back the method in Fragment to call Fragment: FragmentManager. findFragmentById 6. replace, add, and remove methods of Fragment Replace: replace Fragment stack top page add: add Fragment to stack top page remove: remove Fragment stack top page

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.