In Android development, the activity between intent using bundle,fragment between and activityty through the setarguments using bundles, for some simple parameter transfer is relatively simple, and there are many ways, This is not introduced. Here is a description of complex parameter passing, such as passing set ArrayList, Object arraylist<object>.
The bundle is basically the same, whether it is the parameter passing between the activity or the parameter passing between fragment, or between activity and fragment. Bundles here are quite with a medium, tying the data together;
But for some objects to be passed, we need to serialize the objects that are passed, and serialize the objects in two ways:java.io.Serializable and Android.os.Parcelable
Java uses serializable, and Google uses a custom parcelable on Android.
two ways to serialize the difference:
1. When using memory, parcelable than serializable performance, recommend the use of parcelable class;
2.Serializable generates a large number of temporary variables at the time of serialization, resulting in frequent GC;
3.Parcelable can not be used in the case of storing data on disk, because Parcelable does not guarantee the continuity of the data in the case of changes in the outside world, this situation is recommended to use serializable.
Activity jumps to another activity,
The first step; define a serialized entity class
Serializable way:
public class Studentser implements Serializable {private string name;private int age;private string Habby;private string t Itle;public Studentser () {super ();} Public Studentser (string name, Int. age, String Habby, string title) {super (); this.name = Name;this.age = Age;this.habby = Habby;this.title = title;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Gethabby () {return habby;} public void Sethabby (String habby) {this.habby = Habby;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} @Overridepublic String toString () {return "Student [name=" + name + ", age=" + Age + ", habby=" + habby+ ", title=" + titl E + "]";}}
Second step; Passing the serialized object object
Click to jump to the second acitivityintent intent = new Intent (Mainactivity.this,secondactivity.class); Bundle bundle = new Bundle (), bundle.putserializable ("Data", "data"), Intent.putextras (bundle), startactivity (intent);
Third step; Receive serialized object
Gets the data that the bundle passes over intent intent = getintent ();d ata = (arraylist<studentser>) Intent.getserializableextra ("Data") );
Activity passing parameters to fragment
parcelable mode
first step ; Define a serialized entity class
public class Studentpar implements parcelable {private string name;private int age;private string habby;private string tit Le;public Studentpar () {super ();} Public Studentpar (string name, Int. age, String Habby, string title) {super (); this.name = Name;this.age = Age;this.habby = Habby;this.title = title;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;} Public String Gethabby () {return habby;} public void Sethabby (String habby) {this.habby = Habby;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} @Overridepublic String toString () {return "Student [name=" + name + ", age=" + Age + ", habby=" + habby+ ", title=" + titl E + "]";} @Overridepublic int describecontents () {//TODO auto-generated method Stubreturn 0;} public static final parcelable.creator<studentpar> Creator = new creator<studentpar> () {@Overridepublic StudenTpar Createfromparcel (Parcel source) {//TODO auto-generated method Stubstudentpar stu = new Studentpar (); stu.name = Sourc E.readstring (); stu.age = Source.readint (); stu.habby = Source.readstring (); stu.title = Source.readstring (); return Stu;} @Overridepublic studentpar[] NewArray (int size) {//TODO auto-generated method Stubreturn New studentpar[size];}};/ * * Entity class data written Parcel */@Overridepublic void Writetoparcel (Parcel dest, int flags) {//TODO auto-generated method Stubdest.wri Testring (name);d Est.writeint (age);d est.writestring (habby);d est.writestring (title);}}
Second step, passing the serialized object
Fragment Fragment = new Myfragment (); Fragmentmanager manager = Getsupportfragmentmanager (); Fragmenttransaction ft = manager.begintransaction (); Bundle bundle = new Bundle (), Bundle.putparcelablearraylist ("Fragdata", fragdata);//("Fragdata", fragdata); Fragment.setarguments (bundle); Ft.replace (R.id.fram, Fragment, "myfragment"); Ft.commit ();
Third step; receive parameters
Bundle bundle = Getarguments (); arraylist<studentpar> fragdata = bundle.getparcelablearraylist ("Fragdata");
In summary, passing objects can beserializable and parcelable, both between activity and fragment, or between activity and fragment, are all the same, remember that the object being passed is to be implemented Serializable and parcelable;serializable is relatively much simpler. Which method to use depends on the situation.
All right, welcome to the exchange. The demo is attached as follows:
Click the Open link http://download.csdn.net/detail/kern_/8738587
Reproduced please show the source;
Transfer complex parameters in Android, between activity using intent between bundle,fragment and Activityty by setarguments using bundles