The Android foundation USES the Fragment control to switch between multiple pages

  • 2020-05-17 06:27:41
  • OfStack

Today we will talk about the control of Fragment, mainly switching View and page replacement. Then there is how to get the management objects of Fragment and how to communicate with Activity.

1. Manage Fragment
To manage fragment in activity, you need to use FragmentManager. Get an instance of it by calling getFragmentManager() of activity.

The & # 8226; You can do some things with FragmentManager, including using findFragmentById()(fragment for providing one UI in activity layout) or findFragmentByTag(for fragment with or without UI) to get fragment present in activity.
The & # 8226; Pop fragment off the background stack, using popBackStack() (impersonate the user by pressing the BACK command).
The & # 8226; Use addOnBackStackChangeListener() to register an listener that listens for background stack changes.

2. Handle Fragment affairs
One strong feature about using fragment in activity is that fragment can be added, removed, replaced, and other actions performed based on user interactions. Each set of changes committed to activity is called a transaction and can be processed using API in FragmentTransaction. We can also save every transaction to backstack managed by activity, allowing the user to navigate back through the changes of fragment (similar to navigating back through activity).

Get 1 instance of FragmentTransaction from FragmentManager:


FragmentManager fragmentManager =getFragmentManager();
FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();

Each transaction is a set of changes to be executed simultaneously. You can set all the changes you want to make in a given transaction, using things like add(), remove(), and replace(). Then, to apply a transaction to activity, you must call commit().

Before calling commit(), you might want to call addToBackStack() to add the transaction to the backstack of an fragment transaction. This back stack is managed by activity and allows the user to return to the previous fragment state by pressing the BACK button.


// Create a modification instance 
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);
// Commit changes 
transaction.commit();

Above is how to replace one fragment with another and leave the previous state in the background 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 back stack, so the user can back out of 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 application changes before you call commit() will be added to the background stack as a single transaction, and the BACK button will push them back and forth. The order in which changes are added to FragmentTransaction is not important, with the following exceptions:

The & # 8226; commit() must be called last
The & # 8226; If multiple fragment are added to the same container, the order in which they are added determines the order in which they are displayed in view hierarchy

When executing a transaction that removes fragment, if addToBackStack() is not called, that fragment is destroyed when the transaction commits and the user cannot navigate back to it. In view of this, when removing an fragment, if addToBackStack() is called, fragment will be stopped, and if the user navigated back, it will be restored. In addition, for each fragment transaction, you can apply a transaction animation by calling setTransition() before committing the transaction.

Calling commit() does not immediately execute the transaction. Instead, it schedules the transaction, and once it's ready, it runs on activity's UI thread (the main thread). If necessary, however, you can call executePendingTransactions() from your UI thread to immediately execute the transaction committed by commit(). But this is usually not necessary unless the transaction is a slave to a task in another thread.
Warning: you can only commit transactions using commit() before activity saves its state (when the user leaves activity).

3. Communicate with Activity
Although Fragment is implemented as one Activity independent object and can be used in multiple activity, a given instance of fragment is directly bound to the activity containing it. fragment in particular can access Activity instances using getActivity() and can easily perform tasks such as finding an view in activity layout. The following code:


View listView =getActivity().findViewById(R.id.list);

Similarly,activity can call methods in fragment by getting a reference from FragmentManager to Fragment, using findFragmentById() or findFragmentByTag().

ExampleFragment fragment =(ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);

4, summarize
Finally, we need to say the example of Fragment. Android has officially provided Demo examples of various USES of Fragment. API Demo below SDK contains various use examples of Fragment. It separates different functions and implements different classes. You can view the code as needed.


Related articles: