Android fragement pass parameters to each other

Source: Internet
Author: User

When loading fragment in activity, sometimes to use multiple fragment to switch, and transfer value to another fragment, that is, two fragment between the parameters of the transfer, a lot of data, find two methods, One is passing through a common activity, and this method is to add a field to the activity to temporarily save some values.

Actually, what I started thinking about was using Sharedpreferences, but think too much trouble!

And then flipping through the fragment API and finding another way to pass it, just like the activity, is to implement a piece of code in a parameter-passing program, which is passed through bundles, in contrast to the first method.


Fragment parameter passing method one

Define a field in the activity, then add the set and get methods, code as follows, Mtitle is the argument to pass, if it is a transitive object, you can change Mtitle to an object

public class Demoactivity {private string Mtitle;public string Getmtitle () {return mtitle;} public void Setmtitle (String title) {this.mtitle = title;}}


Fragment call method, it should be noted that when setting the value of a strong turn

((demoactivity) getactivity ()). Getmtitle ();


Fragment parameter passing method two

You can use bundles for parameter passing, so you can take parameters when you jump two fragment, and you can pass a complex object as well.

Ft.hide (Getactivity (). Getsupportfragmentmanager (). Findfragmentbytag (""));D emofragment demofragment = new Demofragment ();  Bundle bundle = new bundle ();  Bundle.putstring ("Key", "This is Method two");  Demofragment.setarguments (bundle);  Ft.add (R.id.fragmentroot, demofragment, searchproject);  Ft.commit ();  


In the other fragment the way to get the parameters only need a statement, key is its own definition of an identity, the form of parameters as long as the bundle can be passed can be implemented

String string = Getarguments (). getString ("key");  



Summarize

From the above two methods, the method of a simple, but personally feel a bit opportunistic, but also very close to Java development specifications, if it is the same activity to load a lot of fragment, this method is undoubtedly much simpler

And the second method is to use the Android bundle transfer, this method should be more official, but in the project development, how to achieve simple how to come, there is not much relationship

Have any questions or want to get more code, you can comment on the HA

Android fragement pass parameters to each other

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.