Android uses intent to pass objects (using serializable or Parcelable) between activities

To transfer objects between different activities in Android, we can consider using bundle. Putserializable (key, object); Bundle.putparcelable (key, object) can also be considered; The object in the former method should implement the serializable interface, and the object in the latter method should implement the Parcelable interface. Let's illustrate it with a complete example.

1. Create a new Android project. The directory structure of the project is as follows:

2. Modify the main.xml layout file. The source code of the layout file is as follows:

3. Create a new entity class package under the SRC directory of the project, named com.andy.entity. At the same time, add two entity classes to the package, one is person.java, which implements the serializable interface; One is police.java, which implements the Parcelable interface. The codes are as follows:

4. Modify the testactivity.java class in the package com.andy.testdemo, and add the classes serializabledemo and parcelabledemo to the package, which inherit the activity class and display the data of person object and police object respectively. The code is as follows:

Serializabledemo.java class

Parcelabledemo.java class:

5. Register the two newly added activities in the androidmanifest.xml file.

6. Run the program to view the renderings:

【1】 Screenshot of main interface:

【2】 The effect of clicking the serializable button

【3】 The effect of clicking the Parcelable button

=========================================================================The above is an example of how to use intent to transfer objects between different activities.

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
分享
二维码
< <上一篇
下一篇>>