Common methods and application scenarios of FragmentTrasaction

1. Common methods of Fragment Trasaction

How do the add, remove, replace, hide, show, replace, attach, detach methods of FragmentTrasaction affect the life cycle of Fragment?

First look at the Fragment life cycle:

The experiment is as follows:

  1. add() 

    insert image description here

  2. remove()insert image description here
  3. replace() is equivalent to remove and then add
  4. hide() lifecycle method will not be called backinsert image description here
  5. show() lifecycle method will not be called backinsert image description here
  6. detach()insert image description here
  7. attach()insert image description here

Note: The onClick log is the result returned by Fragment's isAdd() method after the corresponding method is executed.

2. Fragment management application scenarios 

1. Best practice for Fragment management:

add, hide and show, and complete the initialization work when switching between Fragments through onHiddenChanged.
Practice goals:
(1) When each Fragment display state is switched, it will neither destroy and rebuild the object, nor destroy and rebuild the layout (
2) Complete the switching between Fragments through onHiddenChanged and complete the initialization that needs to be done in onResume when the front and back switch is completed Work, making up for the problem that the initialization work cannot be fully synchronized when switching between Fragments caused by inconsistent life cycles

2. Fragment management practice two:

replace+addToBackStack(null)

Fragment management can be divided into the same level type and the flow type in terms of operation process:

  • Same-level Fragment : For example, the main interface of QQ, messages, contacts, and dynamics. These three Fragments belong to the same-level relationship. The Fragment of the main interface in our usual projects also belongs to the same-level Fragment 

  • Process Fragment : For example, my example Demo can be understood as a user account process, which can include: login/registration module----forget/retrieve password module----user agreement module, these Fragents belong to process Fragment

Practice goals:
(1) All displayed fragment objects are in memory, and the replaced fragment is the layout destroyed, but the object is not completely destroyed (2
) When the replaced layout is redisplayed, the statement cycle starts from onCreateView –…–onResume, except that the fragment object does not re-onAttach and onCreate, you can return to the previous fragment through popBackStack.

Summarize:

  • The method of add, hide, show+onHiddenChanged is suitable for the needs of fragments at the same level. If there are 3 or less fragments, data preloading can be ignored if there is not much data. If there is more data, data preloading can be canceled;
  • The replace+addToBackStack(null) method is suitable for process-based fragment management;
  • viewpager+fragment: suitable for multiple fragments at the same level.

 

Quote:

1.  How does the method of FragmentTransaction affect the life cycle of Fragment

2.  Best practice of tab+Fragment management

 

 

Guess you like

Origin blog.csdn.net/lrxb_123/article/details/105126267