How to pass objects in intent in Android, as far as I know there are two methods, one is bundle.putserializable (Key,object), the other is bundle.putparcelable (Key, Object) Of course, these object is a certain condition, the former is the implementation of the serializable interface, and the latter is the implementation of the Parcelable interface, in order to make it easier for everyone to understand I still write a simple demo, everyone step by step with me!
The first step: new Android project named Objecttrandemo (more than a class!) The directory structure is shown below:
Step Two: Modify the Main.xml layout file (I added two buttons here) code as follows
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=
"http://schemas.android.com/apk/" Res/android "
android:orientation=" vertical "
android:layout_width=" fill_parent "
android:layout_" height= "Fill_parent"
>
<textview
android:layout_width= "fill_parent"
android:layout_ height= "Wrap_content"
android:text= "Welcome to Mr Wei ' s blog."
/>
<button
android:id= "@+id/button1"
android:layout_width= "Fill_parent"
android: layout_height= "Wrap_content"
android:text= "Serializable"/> <button android:id=
"@+id
" /button2 "
android:layout_width=" fill_parent "
android:layout_height=" wrap_content "
android:text=" "Parcelable"
/>
</LinearLayout>
Step three: Create two new classes one is Person.java implement Serializable interface, another Book.java implement Parcelable interface, the code is as follows:
Person.java:
Package Com.tutor.objecttran;
Import java.io.Serializable;
public class Person implements Serializable {
private static final long Serialversionuid = -7060210544600464481l;
private String name;
private int age;
Public String GetName () {return
name;
}
public void SetName (String name) {
this.name = name;
}
public int getage () {return age
;
}
public void Setage (int age) {
this.age = age;
}
}