테랑입니다. 오늘은 Java 의 Serializable 에 이어서 Android 에서 사용되는 Parcelable 에 대해 알아 보도록 하겠습니다.
직역하면 소포, 꾸러미 라는 뜻을 가는 Parcelable 은 프로세스 간의 통신을 위해 메시지를 전달할 뿐만 아니라, 프로세스의 세션 간 데이터 저장 및 복구에도 사용 가능합니다.
일반적으로 Android 에서 Intent 에 대량(?) 의 Data 를 전달할 때 사용되곤 합니다.
아래 Parcelable 예제를 보시기 바랍니다.
Parcelable Class Source
public class Rect implements Parcelable{
public int left; public int right; public int top; public int bottom;
public Rect(Parcel in){
redFromParcel(in);
}
// 아래 코드에서 사용할 생성자
public Rect(int left, int right, int top, int bottom){
this.left = left;
this.right = right;
this.top = top;
this.bottom = bottom;
}
public int describeContents(){
return 0;
}
public int getLeft(){
return this.left;
}
public int getRight(){
return this.right;
}
public int getTop(){
return this.top;
}
public int getBottom(){
return this.bottom;
}
// Write 하는 순서를 주의해야 한다. public void writeToParcel( Parcel dest, int flags ){
dest.writeInt( left ); dest.writeInt( right ); dest.writeInt( top ); dest.writeInt( bottom );
}
// 정적 객체 CREATOR 를 제공해야 한다. 저장된 정보를 Parcel 로 부터 읽어들여 객체를 생성하는 역할을 함. public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>(){
// Read 하는 순서에 주의해야 한다.
public Rect createFromParcel( Parcel source ){
Rect r = new Rect(); r.left = source.readInt(); r.right = source.readInt(); r.top = source.readInt(); r.bottom = source.readInt(); return r;