-
INITIALIZING initial state
-
CREATED created status
-
ACTIVITY_CREATED fully created but not started
-
STARTED creates and starts, visible and inoperable
-
RESUMED creation is started and operable [picture uploading... (image-1940e7-1557923091355-0)]
This article only explains the CREATED, STARTED and RESUMED states. Since the mState and Lifecycle.State defined in the Fragment are not the same state, they are regarded as the same concept in this article;
Correspondence with life cycle
You must know that Fragment lifecycle has onDestory, onStop and other methods, but there are not so many states. How to identify states and corresponding relationships is given below;
First, I regard life cycle methods as sorting from small to large from oncerate - > oncreateview - > OnStart - > onresume - > onpause - > onstop - > ondestoryview - > ondestory;
Similarly, we regard life cycle states created - > started - > resumed as sorted from small to large;
- CREATED status
CREATED is the CREATED state. In a narrow sense, the lifecycle method goes to onCerate. If the current fragment state is greater than CREATED, the fragment lifecycle method will go to onDestoryView. If it is less than CREATED, it will go to onCerate; Therefore, there are two situations for CREATED;
- STARTED status
Similarly, there are two situations in the STARTED state. If the current fragment state is greater than STARTED, the fragment lifecycle method will go to onPause; if it is less than CREATED, it will go to onStart;
- RESUMED status
The status of "RESUMED" is special. It only represents the onResume status. Whether it is large or small, it will eventually stay in the onResume status;
The above life cycle state reversal conclusion is extracted based on the FragmentManagerImpl.moveToState() method. Please advise if it is misleading
How to use
setMaxLifecycle can be used alone or in combination with add and other methods. First, we analyze the state changes of executing the add command alone:
Perform the add operation separately
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frame_layout,cardFragment); fragmentTransaction.commit();
add with setMaxLifecycle(Lifecycle.State.CREATED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frame_layout,cardFragment); fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.CREATED); fragmentTransaction.commit();
add with setMaxLifecycle(Lifecycle.State.STARTED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frame_layout,cardFragment); fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.STARTED); fragmentTransaction.commit();
add with setMaxLifecycle(Lifecycle.State.RESUMED)
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction(); fragmentTransaction.add(R.id.frame_layout,cardFragment); fragmentTransaction.setMaxLifecycle(cardFragment, Lifecycle.State.RESUMED); fragmentTransaction.commit();
Using setMaxLifecycle alone
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); fragmentTransaction.setMaxLifecycle(cardFragment, xxx); fragmentTransaction.commit();
- Perform the create operation on the Fragment in the RESUMED state
- Start the Fragment in the RESUMED state
- Operate the Fragment in the RESUMED state, create, and start
Due to space reasons, you will not introduce various combinations one by one. As long as you understand the life cycle state, whether the state is rising or falling, whether combined or used alone, you can control it;
FragmentPagerAdapter changes
Because setMaxLifecycle brings life cycle settings and replaces the old setUserVisibleHint method, it is also adapted in the FragmentPagerAdapter
FragmentPagerAdapter
public static final int BEHAVIOR_SET_USER_VISIBLE_HINT = 0; public static final int BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT = 1; private final int mBehavior; public FragmentPagerAdapter(@NonNull FragmentManager fm) { this(fm, BEHAVIOR_SET_USER_VISIBLE_HINT); } public FragmentPagerAdapter(@NonNull FragmentManager fm,@Behavior int behavior) { mFragmentManager = fm; mBehavior = behavior; }
The latest FragmentPagerAdapter uses an mBehavior to control either setUserVisibleHint or setMaxLifecycle; MBehavior is specified in the construction method;
As can be seen from the code, setMaxLifecycle(mCurrentPrimaryItem, Lifecycle.State.STARTED) is used to replace setUserVisibleHint(false), and setMaxLifecycle(fragment, Lifecycle.State.RESUMED) is used to replace setUserVisibleHint(true);
Why use Lifecycle.State.STARTED? Because the combination of add+Lifecycle.State.STARTED and attach+Lifecycle.State.STARTED is essentially used here;
The final result is that the invisible Fragment will only go to the onStart method in the life cycle, not the onResume method;
Lazy load new scheme
To sum up, setUserVisibleHint was used to control the lazy loading of fragments in the past. There is a new idea in the latest version of FragmentPagerAdapter, which can be switched to BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT mode is judged in FragmentonResume, which is more in line with the display logic;
Switch to behavior_ RESUME_ ONLY_ CURRENT_ For fragment mode, you need to call the construction method of two parameters:
new FragmentPagerAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)
`Mode, you need to call the construction method of two parameters:
new FragmentPagerAdapter(getSupportFragmentManager(),FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT)