There are two methods to transmit data between Fragment and Activity: setArgument and interface callback. Next we will first learn the first method.
(1) UseSetArgumentMethod:
For ease of understanding, I would like to give a metaphor here: if Activity is an emperor, it has set up three departments (for example, three provinces and six departments): Fragment1, Fragment2 and Fragemnt3;
Now he wants to instruct the Department Fragment1 to do some things, for example, leading troops to attack the island !! Well, it certainly did not go to the Department by itself.
In general, there will be a prime minister or the eunuchs in charge of the Emperor's statement, right?FragmentManager. Then, of course,
The head of the eunuch will not run in person.FragmentTransaction In general,
It is responsible for three things:Add (), remove (), replace (),Add a department, withdraw a department, and replace a department according to the Emperor's intention.
Now the eunuch has a new task, that is, adding a department and telling it to attack the island. Okay. Put the content"Attach troops to attack the island"In the envelope
(The envelope isBundle). Note that this is a secret order. Of course you need to mark something, that is, addTagHere we specify"179521".
Finally, it is bound to the pigeon and sent out (commit ()).
@ Overridepublic void onClick (View arg0) {if (arg0.getId () = R. id. send_data_btn) {LeftFragment fragment = new LeftFragment (); Bundle bundle = new Bundle (); bundle. putString ("order", "Attach troops to the island"); fragment. setArguments (bundle); FragmentManager fragmentManager = getFragmentManager (); // start the Fragment transaction FragmentTransaction fTransaction = fragmentManager. beginTransaction (); // Add the Fragment to the transaction and specify a TAG fTransaction. add (fragment, "179521"); // submit the Fragment transaction fTransaction. commit ();}}
The metaphor above is not very appropriate. Now we can explain it in a formal language:
If you are familiar with the lifecycle of an Activity, you should know that each application has a unique Task. The Task has a rollback stack backstack to store the window instance objects of the Activity;
Since Fragment also has a window instance object, it also needs to enter the rollback stack when it appears.
With Fragment, Android allows users or developers to manage Fragment, such as adding, removing, and replacing Fragment. There is a class from the end of Android3.0,
FragmentManager is used to manage Fragment, but instead of executing specific actions, it uses an object, FragmentTransaction,
It should be realized thatEachFragmentTransactionIs a set of changes executed at the same time.For example, add two shards at the same time and then delete one of them.
This is all executed at the same time. Use the add (), remove (), replace () Methods to add all the required changes, and then call the commit () method to apply these changes.
Before the commit () method, you can call addToBackStack () to add the transaction to the backstack.
Management. When you press the return key, the status of the last fragment is returned.
Receive data in Fragment:
Get the FragmentManager and retrieve the corresponding content based on the Tag and key.
@ Overridepublic void onClick (View arg0) {if (arg0.getId () = R. id. get_data_btn) {String name = getFragmentManager (). findFragmentByTag ("179521 "). getArguments (). getString ("order"); Toast. makeText (getActivity (), "command is" + name, Toast. LENGTH_SHORT ). show ();}}
The result is as follows:Download