Android parcelable Object Generation: Parcelablegenerator

Source: Internet
Author: User

GitHub Address: Https://github.com/baoyongzhang/ParcelableGenerator


Analysis:

The project is a compile-time annotation using the annotation, i.e. @retention (Retentionpolicy.class)

@inherited annotations are not used at the same time, so add @parcelable on both the parent and subclass that need to be serialized


Refer to Readme.md below


Parcelablegenerator Introduction

Parcelablegenerator can convert any object to the Parcelable type, facilitating the transfer of objects.

In Android, there are generally two ways to serialize objects, one is serializable, and the other is parcelable.

    • Serializable exists in Java and is less efficient.
    • Parcelable is available in Android and is the official recommended way, much more efficient than serializable.

Although parcelable efficiency is high, but use more trouble than serializable, many people do not use parcelable is because the writing is too cumbersome, especially when the property is particularly large, we will each attribute Parcel.write () Then in Parcel.read () back, rather cumbersome, as serializable simple rough, directly effective.

Parcelablegenerator can solve the problem of parcelable use, so the simplicity of using parcelable can be compared with the use of serializable.

How to use

For example, we have a user class that is used to hold some information about our users, and we need to use @parcelable to decorate the class, noting that @parcelable-decorated classes must have a public-no-parameter construction method.

Import com.baoyz.pg.Parcelable;@parcelable Public class User{Private StringNamePrivate intAge Public String GetName() {returnName } Public void SetName(String name) { This.Name=Name } Public int Getage() {returnAge } Public void Setage(int  Age) { This.Age=Age }}

We're going to pass a user object through intent to an activity called showuseractivity. We need to call the Intent.putextra () method to pass the object in, which is definitely not possible, and we need to call the Pg.createparcelable () method to convert the object to parcelable in the incoming intent.

Import Com.baoyz.pg.PG;//Simulate creating an object and setting the property valueUserUser= New User(); user.SetName ( "zhangsan" ); user.Setage ( -);IntentIntent= New Intent( This,showuseractivity.Class);//Call PG to convert the object to parcelable, passed into the intentIntent.PutExtra ( "user" ,PG.Convertparcelable (user)); startactivity (intent);

Get the user object in Showuseractivity, without having to write any translated code, directly getintent (). Getparcelableextra () assigns a value to the original object type variable.

 Public class showuseractivity extends Activity{@Override    protected void onCreate(Bundle savedinstancestate) {Super.OnCreate (savedinstancestate);//Get the original object type directly        UserUser=Getintent ().Getparcelableextra ( "user" );//Get property valueUser.GetName (); User.Getage (); }}

The above may be an error, you can use the following two ways

User user = Pg.unconvert (Intent.getparcelableextra ("user"));
User user = (user) Intent.getparcelableextra ("User");
Update Introduction to version 2.0
    • Fix bugs, use basic data types to wrap classes can cause problems, and so on.
    • Add @ParcelIgnore annotations, and modify the field above the model to omit it from being serialized.
    • Easier to use, when a property in the model is a different object, or a list contains other objects, the class of the object is declared without the need to convert the code after the @Parcelable declaration.
//When the property that passes the object contains other objects, or is a list, and the object in the list does not support serialization, the direct pass will appear null//workaround, use @parcelable to modify classes that do not support serialization//e.g. a classroom objectClassroomThe= New Classroom();//Classroom contains a teacher, teacher class with @parcelable decorationTeacherTeacher= New Teacher( "teachername" );//Assign the teacher's object directly to the classroomThe.Setteacher (teacher);//For example, the classroom contains a lot of students, using list to save, student class with @parcelable decorationlist<Student>Students= New arraylist<Student>();//Create student object directly to listStudents.AddNew Student( "stu1" )); students.AddNew Student( "stu2" )); students.AddNew Student( "stu3" ));.Setstudents (students);//Pass the Classroom object, call the conversion method, and the teacher, and the student object in the list are automatically converted to parcelable type and passedIntent.PutExtra ( "classroom" ,PG.Convertparcelable (guest);

Android parcelable Object Generation: Parcelablegenerator

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.