Deploy Parcelable via AIDL
First step: - Create another .aidl file that will be used to define the Student class (Parcelable class).
(Student.aidl) package com.aidl; parcelable Student;
we write this because helpl can detect the student class.
Second step: now you need to define a java class called student and implement the parcable interface in this class. parcable has two abstract methods that you must implement in your class.
import android.os.Parcel; import android.os.Parcelable; public class Student implements Parcelable { public String name; public String father_name; public Student(Parcel source) { name = source.readString(); father_name = source.readString(); } public Student() {} public void setName(String name) { this.name = name; } public void setFatherName(String father_name) { this.father_name = father_name; }
// methods of the transfer interface
@Override public int describeContents() {
In any class that implements Parcelable, you must provide the CREATOR field. The type of CREATOR must be Parcelable.Creator. Here, instead of T, we write the name of our class, for example. Student. CREATOR is used during the UnMarshalling object.
It has two methods -
1-T createFromParcel(Parcel parcel) :This method is called when UnMarshalling happen during receiving the data. Keep care that we receive the data member in same sequence as we write in writeToPacel(). Here we create a constructor in which we demarshalling the data. 2-NewArray(int size) : Here we just create an array of given size and return. public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { @Override public Student createFromParcel(Parcel source) { // TODO Auto-generated method stub return new Student(source); } @Override public Student[] newArray(int size) { // TODO Auto-generated method stub return new Student[size]; } };
for more information: check here
source share