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 setargument Method:

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

now he's going to command the department . Fra Gment1 to do something, such as: "lead the Army to attack the island!!" Well, it certainly does not run to tell the department by itself.

generally speaking, there will be a prime minister or a eunuch to take charge of the Emperor Guardian . Fragmentmanager . Then, of course,

The eunuch is not going to run himself, and there is a name fragmenttransaction The affairs of the eunuch, in general,

It is responsible for three things: Add (), remove (), replace () , in accordance with the emperor's meaning to add a department, 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.

(envelope is Bundle ). Notice that this is Chitian, of course, to make a mark or something to tag, is to 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're familiar with activity tas k , task There's a fallback stack backstack , storage activity

because Fragment also has a window instance object, so it is also going to go to the fallback stack when it appears.

Use Fragment , Android To allow the user or developer to perform the Fragment management, such as additions, removal and replacement. There is a class from Android3.0 ,

Fragmentmanager dedicated to managing Fragment , but it does not perform specific actions in person, but instead uses an object, fragment transaction, fragmenttransaction ,

It should be recognized that eachfragmenttransactionis a set of changes that are executed at the same time. such as adding2aFragment, and then delete one of them.

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.

in the Frag ment Receive data:

Get Fra Gmentmanager , and then according to the label Tag and the Key remove the corresponding content.

<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:




Write back the callback method tomorrow.

First, upload the demo example of the first method.

Routine download




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.