Two methods of Android intent passing objects (serializable and Parcelable) are introduced in detail

Two methods of Android intent passing objects (serializable and Parcelable) are introduced in detail

Today, I want to tell you how to transfer objects in intent in Android. As far as I know, there are two methods: bundle. Putserializable (key, object); The other is bundle. Putparcelable (key, object); Of course, these objects have certain conditions. The former implements the serializable interface and the latter implements the Parcelable interface. In order to make it easier for you to understand, I still wrote a simple demo as usual. Let's follow me step by step!

Step 1: create a new Android project named objecttrandemo (there are many classes!). The directory structure is as follows:

Step 2: modify the main.xml layout file (I added two buttons here). The code is as follows

Step 3: create two new classes. One is person.java to implement the serializable interface, and the other is book.java to implement the Parcelable interface. The codes are as follows:

Person.java:

Book.java:

Step 4: modify objecttrandemo.java and create two new activities, one is objecttrandemo1.java and the other is objecttrandemo2.java. They are used to display person object data and Book object data respectively. The codes are as follows:

ObjectTranDemo.java:

ObjectTranDemo1.java:

ObjectTranDemo2.java:

Step 5: an important step, modify the androidmanifest.xml file (add two new activities, objecttrandemo1 and objecttrandemo2), and declare the code as follows (lines 14 and 15):

Step 6: run the above project to view the renderings:

Effect 1: first interface:

Effect 2: click the serializable button

Effect 3: click the Parcelable button:

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>