Add fragments dynamically
The real power of fragments is that they can be dynamically added to activities while the program is running. Add fragments dynamically according to the specific situation, you can customize the program interface more diversified.
Dynamically adding fragments is mainly divided into 5 steps.
1. Create an instance of the fragment to be added.
2. Get the FragmentManager, you can directly call the getFragmentManager() method in the activity to get it.
3. To start a transaction, start by calling the beginTransaction() method.
4. Adding fragments to the container is generally implemented using the replace() method. The id of the container and the fragment instance to be added need to be passed in.
To submit the transaction, call the commit() method to complete.
Example:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit();
simulating the return stack in the fragment
add by clicking the button After a fragment, press the Back key at this time and the program will exit directly. If we want to imitate the effect similar to the return stack, press the Back key to return to the previous fragment, how to achieve it? In fact, it is very simple. FragmentTransaction provides an addToBackStack() method, which can be used to add a transaction to the return stack and modify the code in MainActivity, as shown below:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction()< span class="hljs-comment">; transaction.replace(R.id< span class="hljs-preprocessor">.right_layout, fragment); transaction.addToBackStack(null);//The addToBackStack() method of FragmentTransaction is called before the transaction is submitted. It can receive a name to describe the state of the return stack. Generally, you can pass in null. transaction.commit();
Second, the life cycle of the fragment
Status and callbacks of fragments
Each activity has a total of running state, paused state, stopped state and destroyed state during its life cycle. Similarly, each fragment may experience these states during its life cycle, but there will be some differences in some small places.
1. Running status: When a shard is visible and the activities associated with it are running, the shard is also running.
2. Paused state: When an activity enters the paused state (because another activity that does not fill the screen is added to the top of the stack), the visible fragments associated with it will enter the paused state.
3. Stop state: When an activity enters the stop state, the fragments associated with it will enter the stop state. Or by calling the remove() and replace() methods of FragmentTransaction to remove the fragments from the activity, but if the addToBackStack() method is called before the transaction is committed, the fragments will also enter the stopped state at this time. In general, the fragments that enter the stopped state are completely invisible to the user and may be recycled by the system.
4. Destroyed state: Fragments always exist depending on the activity, so when the activity is destroyed, the fragments associated with it will enter the destroyed state. Or remove the fragments from the activity by calling the remove() and replace() methods of FragmentTransaction, but the addToBackStack() method is not called before the transaction is committed, and the fragments will also enter the destroyed state at this time.
The Fragment class also provides a series of callback methods to cover every part of the fragment life cycle. Among them, there are almost all callback methods in the activity, but the fragments also provide some additional callback methods, so let’s focus on these callbacks.
1.onAttach(): Called when the fragment and the activity are associated.
2. onCreateView(): Called when creating a view for the fragment (loading the layout).
3. onActivityCreated(): It is called when the activity associated with the fragment must have been created.
4. onDestroyView(): Called when the view associated with the fragment is removed.
5. onDetach(): Called when the fragment is disassociated from the activity.
For the complete life cycle diagram of the fragment, please refer to Figure 4.8, which is from the official Android website.
Experiencing the life cycle of fragments
——See section 4.3.2 of the book——
It’s also worth mentioning that in the fragment You can also save data through the onSaveInstanceState() method, because the fragments that enter the stopped state may be recycled when the system memory is insufficient. You can retrieve the saved data in the three methods onCreate(), onCreateView(), and onActivityCreated(). They all contain a savedInstanceState parameter of Bundle type. I will not give the specific code here. If you forget how to write it, please refer to section 2.4.5.
Three, techniques for dynamically loading layouts
Use qualifiers
If you If you use tablets frequently, you should find that many tablet applications are now in dual-page mode (the program will display a list of sub-items on the left panel, and the content on the right panel), because the tablet The screen is large enough to display the content of the next two pages at the same time, but the screen of the mobile phone can only display the content of one page at a time, so the two pages need to be displayed separately.
So how can we determine whether the program should use dual-page mode or single-page mode at runtime? This requires the help of qualifiers (Qualifiers) to achieve.
Some common qualifiers in Android can refer to the following table.
Use minimum width qualifier
The minimum width qualifier allows us to specify a minimum for the width of the screen. (Using dp as the unit), and using this minimum value as the critical point, a device with a screen width greater than this value loads one layout, and a device with a screen width smaller than this value loads another layout. It should be noted that the minimum width qualifier was introduced in Android 3.2.
Add fragments dynamically
The real power of fragments is that they can be dynamically added to activities while the program is running. Add fragments dynamically according to the specific situation, you can customize the program interface more diversified.
Dynamically adding fragments is mainly divided into 5 steps.
1. Create an instance of the fragment to be added.
2. Get the FragmentManager, you can directly call the getFragmentManager() method in the activity to get it.
3. To start a transaction, start by calling the beginTransaction() method.
4. Adding fragments to the container is generally implemented using the replace() method. The id of the container and the fragment instance to be added need to be passed in.
To submit the transaction, call the commit() method to complete.
Example:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction(); transaction.replace(R.id.right_layout, fragment); transaction.commit();
simulating the return stack in the fragment
add by clicking the button After a fragment, press the Back key at this time and the program will exit directly. If we want to imitate the effect similar to the return stack, press the Back key to return to the previous fragment, how to achieve it? In fact, it is very simple. FragmentTransaction provides an addToBackStack() method, which can be used to add a transaction to the return stack and modify the code in MainActivity, as shown below:
AnotherRightFragment fragment = new AnotherRightFragment(); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction transaction = fragmentManager. beginTransaction()< span class="hljs-comment">; transaction.replace(R.id< span class="hljs-preprocessor">.right_layout, fragment); transaction.addToBackStack(null);//The addToBackStack() method of FragmentTransaction is called before the transaction is submitted. It can receive a name to describe the state of the return stack. Generally, you can pass in null. transaction.commit();
Second, the life cycle of the fragment
Status and callbacks of fragments
Each activity has a total of running state, paused state, stopped state and destroyed state during its life cycle. Similarly, each fragment may experience these states during its life cycle, but there will be some differences in some small places.
1. Running status: When a shard is visible and the activities associated with it are running, the shard is also running.
2. Paused state: When an activity enters the paused state (because another activity that does not fill the screen is added to the top of the stack), the visible fragments associated with it will enter the paused state.
3. Stop state: When an activity enters the stop state, the fragments associated with it will enter the stop state. Or by calling the remove() and replace() methods of FragmentTransaction to remove the fragments from the activity, but if the addToBackStack() method is called before the transaction is committed, the fragments will also enter the stopped state at this time. In general, the fragments that enter the stopped state are completely invisible to the user and may be recycled by the system.
4. Destroyed state: Fragments always exist depending on the activity, so when the activity is destroyed, the fragments associated with it will enter the destroyed state. Or remove the fragments from the activity by calling the remove() and replace() methods of FragmentTransaction, but the addToBackStack() method is not called before the transaction is committed, and the fragments will also enter the destroyed state at this time.
The Fragment class also provides a series of callback methods to cover every part of the fragment life cycle. Among them, there are almost all callback methods in the activity, but the fragments also provide some additional callback methods, so let’s focus on these callbacks.
1.onAttach(): Called when the fragment and the activity are associated.
2. onCreateView(): Called when creating a view for the fragment (loading the layout).
3. onActivityCreated(): It is called when the activity associated with the fragment must have been created.
4. onDestroyView(): Called when the view associated with the fragment is removed.
5. onDetach(): Called when the fragment is disassociated from the activity.
For the complete life cycle diagram of the fragment, please refer to Figure 4.8, which is from the official Android website.
Experiencing the life cycle of fragments
——See section 4.3.2 of the book——
It’s also worth mentioning that in the fragment You can also save data through the onSaveInstanceState() method, because the fragments that enter the stopped state may be recycled when the system memory is insufficient. You can retrieve the saved data in the three methods onCreate(), onCreateView(), and onActivityCreated(). They all contain a savedInstanceState parameter of Bundle type. I will not give the specific code here. If you forget how to write it, please refer to section 2.4.5.
Three, techniques for dynamically loading layouts
Use qualifiers
If you If you use tablets frequently, you should find that many tablet applications are now in dual-page mode (the program will display a list of sub-items on the left panel, and the content on the right panel), because the tablet The screen is large enough to display the content of the next two pages at the same time, but the screen of the mobile phone can only display the content of one page at a time, so the two pages need to be displayed separately.
So how can we determine whether the program should use dual-page mode or single-page mode at runtime? This requires the help of qualifiers (Qualifiers) to achieve.
Some common qualifiers in Android can refer to the following table.
Use minimum width qualifier
The minimum width qualifier allows us to specify a minimum for the width of the screen. (Using dp as the unit), and using this minimum value as the critical point, a device with a screen width greater than this value loads one layout, and a device with a screen width smaller than this value loads another layout. It should be noted that the minimum width qualifier was introduced in Android 3.2.