Today I will explain the control of Fragment, mainly operations such as switching View and page replacement. There is also how to obtain the management object of Fragment and how to communicate with Activity.
1. Managing Fragments
To manage fragments in an activity, you need to use FragmentManager. Get its instance by calling getFragmentManager() of the activity.
•You can do some things through FragmentManager, Including: Use findFragmentById() (used to provide a UI fragment in the activity layout) or findFragmentByTag() (applicable to fragments with or without UI) to obtain the fragment that exists in the activity.
•Pop the fragment from the back stack, using popBackStack() (simulating the user pressing the BACK command).
•Use addOnBackStackChangeListener() to register a listener that listens to changes in the back stack.
2. Processing Fragment transactions
A very strong feature of using fragments in activities is: adding, removing, replacing, and performing other actions on fragments based on the user's interaction. Each set of changes submitted to an activity is called a transaction and can be handled using the API in FragmentTransaction. We can also save each transaction to an activity-managed backstack, allowing the user to navigate back through changes in the fragment (similar to navigating back through the activity).
Obtain a FragmentTransaction instance from FragmentManager:
FragmentManager fragmentManager =getFragmentManager(); FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
Each transaction is a set of changes to be executed at the same time. You can set all the changes you want to perform within a given transaction, using methods such as add(), remove(), and replace(). Then, to apply the transaction to the activity, commit() must be called.
Before calling commit(), you may want to call addToBackStack() to add the transaction to the backstack of a fragment transaction. This back stack is managed by the activity and allows the user to return to the previous fragment state by pressing the BACK button.
//创建修改实例 Fragment newFragment = newExampleFragment(); FragmentTransaction transaction =getFragmentManager().beginTransaction(); // Replace whatever is in thefragment_container view with this fragment, // and add the transaction to the backstack transaction.replace(R.id.fragment_container,newFragment); transaction.addToBackStack(null); //提交修改 transaction.commit();
The above is how to replace one fragment with another and keep the previous state in the back stack. In this example, newFragment replaces the fragment identified by R.id.fragment_container in the current layout container. By calling addToBackStack(), the replace transaction is saved to the back stack, so the user can roll back the transaction and bring back the previous fragment by pressing the BACK button.
If you add multiple changes to a transaction (such as add() or remove()) and call addToBackStack(), then all of the changes applied before you call commit() Changes will be added to the back stack as a single transaction, and pressing the BACK button will roll them back together. The order in which changes are added to the FragmentTransaction is not important, with the following exceptions:
•Must call commit() last
•If multiple fragments are added to the same container, the order in which they are added determines their placement in the view hierarchy The order shown in
When executing a transaction that removes a fragment, if addToBackStack() is not called, that fragment will be destroyed when the transaction commits, and the user cannot navigate back to it. For this reason, when removing a fragment, if addToBackStack() is called, the fragment will be stopped, and if the user navigates back, it will be resumed. Additionally, for each fragment transaction, you can apply a transaction animation by calling setTransition() before committing the transaction.
Calling commit() does not execute the transaction immediately. On the contrary, it schedules the transaction and, once ready, runs it on the activity's UI thread (the main thread). If necessary, however, you can call executePendingTransactions() from your UI thread to immediately execute the transaction submitted by commit(). But this is usually not necessary unless the transaction is a slave of a task in another thread.
Warning: You can only use commit() to commit a transaction before the activity saves its state (when the user leaves the activity).
3. Communicate with Activity
Although Fragment is implemented as an independent Activity object and can be used in multiple activities, but a given fragment instance is directly bound to the activity that contains it. Special fragments can access the Activity instance using getActivity() and easily perform tasks such as finding a view in the activity layout. As in the following code:
View listView =getActivity().findViewById(R.id.list);
Similarly, the activity can call methods in the fragment by obtaining a reference to the Fragment from the FragmentManager, using findFragmentById() or findFragmentByTag().
ExampleFragment fragment =(ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
4. Summary
Finally, I need to talk about examples of Fragment. Android officials have provided Demo examples of various uses of Fragment. The API Demo under our SDK contains various examples of Fragment. For usage examples, if you need to see the demo, just look at the API Demo program without having to look around. Different functions are separated and different classes are implemented. You can view the specific code as needed.