Transfer data between Android note fragment and activity

Source: Internet
Author: User

There are two ways to pass data between Fragment and activity, one is to use setargument and one is to use an interface callback. Learn the first method below.

(1) using the Setargument method:

For the sake of understanding, I am here to make a metaphor: if the activity is the emperor, it set up three departments (such as the three provinces six), respectively, Fragment1,fragment2 and Fragemnt3;

Now he will command the department Fragment1 to do something, such as: "lead the Army to attack the island!" Well, it certainly does not run to tell the department by itself.

In general, there will be a prime minister or eunuch to take charge of the Emperor Guardian, is it, this eunuch is Fragmentmanager. Then, of course,

The eunuch is not going to run in person, at this time there is a eunuch named fragmenttransaction , in general,

It is responsible for three things:Add (), remove (), replace (), add a department according to the Emperor's meaning, undo a department, and replace a department.

Now the eunuch has a new task, that is, to add a department and then tell it to attack the island. Good. Put the content "troops on the island " into an envelope.

(Envelopes are bundles). Notice that this is Chitian, of course, to make a mark or something to tag, that is, add tag, here we specify is "179521".

Finally tied to the dove to send out (commit ()).



<pre name= "code" class= "java" > @Overridepublic void OnClick (View arg0) {if (Arg0.getid () ==r.id.send_data_btn) { Leftfragment fragment = new Leftfragment (); Bundle bundle = new bundle (); Bundle.putstring ("Order", "attacking the island"); fragment.setarguments (bundle); Fragmentmanager Fragmentmanager = Getfragmentmanager ();//Start fragment transaction fragmenttransaction ftransaction = Fragmentmanager.begintransaction ();//Add fragment to the transaction and specify a tag ftransaction.add (fragment, "179521");// Submit fragment Transaction Ftransaction.commit ();}}

The above analogy is not very appropriate, now in formal language to illustrate the HA:

If you are familiar with the life cycle of activity, you should know that each application has a unique task,task with a fallback stack backstack, which stores the activity's window instance object;

Because fragment also has a window instance object, it also goes to the fallback stack when it appears.

Use Fragment,android to allow users or developers to perform management of fragment, such as Add, remove, and replace. There is a class from Android3.0,

Fragmentmanager is designed to manage fragment, but it does not perform specific actions in person, but instead uses an object, fragment transaction, Fragmenttransaction,

It should be recognized that each fragmenttransaction is a set of simultaneous execution changes. For example, add 2 fragment at the same time and then delete one of the two.

This is all done at the same time. Use the Add (), remove (), replace () method, add all the required changes, and then call the commit () method to apply the changes.

Before the commit () method, you can call Addtobackstack () and add the transaction to the Backstack, which is the back stack by the activity

Managed, when the user presses the return key, it returns to the state of the previous fragment.

Receive data at fragment:

Get Fragmentmanager, then take the corresponding content according to tag tag and key
<pre name= "code" class= "java" > @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 results are as follows:


Transfer data between Android note fragment and activity

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.