Android foundation uses fragment control to switch multiple pages

Today, let's talk about the control of fragment, mainly switching View and page replacement. There is also how to obtain the management object of the fragment and the communication method with the activity. 1. Managing fragments to manage fragments in an activity, you need to use the fragmentmanager. Get its instance by calling getfragmentmanager () of the activity

• you can do some things through the fragmentmanager, including: use findfragmentbyid() (the fragment used to provide a UI in the activity layout) or findfragmentbytag() (for fragments with or without UI) to obtain the fragments existing in the activity. • Pop up the fragment from the background stack and use popbackstack() (simulate the user pressing the back command). • Use addonbackstackchangelistener() to register a listener that listens for background stack changes.

2. Dealing with fragment transactions a strong feature of using fragments in an activity is to add, remove, replace and perform other actions on fragments according to user interaction. Each set of changes submitted to an activity is called a transaction and can be handled using the API in the fragmenttransaction. We can also save each transaction to an activity managed Backstack, allowing users to navigate back through changes in the fragment (similar to navigating back through the activity).

Obtain a fragmenttransaction instance from the fragmentmanager:

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.

If you add multiple changes to a transaction (such as add () or remove ()) and call addtobackstack (), then all the changes of the application before you call commit () will be added to the background stack as a single transaction, and 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 finally call commit (). If multiple fragment is added to the same container, the order of addition determines the order in which they are displayed in view hierarchy.

When executing a transaction to remove a fragment, if addtobackstack() is not called, the fragment will be destroyed after the transaction is committed, and the user cannot navigate back to it. In view of this, when removing a fragment, if addtobackstack() is called, the fragment will be stopped. If the user navigates back, it will be restored. In addition, for every fragment transaction, you can apply a transaction animation by calling setTransition () before committing the transaction.

Calling commit () does not immediately execute the transaction. On the contrary, it schedules transactions and, once ready, runs on the UI thread of the activity (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 of a task in another thread. Warning: you can only use commit() to commit transactions before the activity saves its state (when the user leaves the activity). 3. Communicate with the activity. Although the fragment is implemented as an activity independent object and can be used in multiple activities, a given fragment instance is directly bound to the activity containing it. Special fragments can use getactivity () to access activity instances and easily perform tasks such as finding a view in the activity layout. As shown in the following code:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>