반응형

안드로이드에서 Activity간, 또는 Fragment간 데이터 공유 시 일반적인 방법으로 Intent에 객체를 담아서 전달한다.


이 과정에서 내부에서 기본적으로 제공하는 Wrapper Class의 경우 문제가 되지 않지만, 개발자가 구현한 데이터 클래스의 경우, 객체 전달 시 기본적으로 Parcelable 또는 Serializable을 Implements 하여 구현해야만 Intent를 통한 객체 전달이 가능한다.



본 포스트에서는 Parcelable 구현시 불편함을 해소하기위한 팁을 공유하고자 한다.

여기까지 보면서 Parcelable을 구현함에 있어 어떠한 불편함이나 번거로움이 없다고 느끼셨다면 조용히 뒤로가기를 눌러도 좋다.


그러나 출렁이는 파도를 타고 본 포스트까지 들어왔다면 적어도 후회없는 결과를 얻고 나가리라 믿어 의심치 않다.


사설은 여기까지만. 다시 주제로 넘어와서 Parcelable 구현의 불편함을 코드로 보자.

아래 코드는 예제 코드로 TestData 클래스를 Parcelable을 implements하여 구현하였다.

import android.os.Parcel;
import android.os.Parcelable;

public class TestData implements Parcelable {

public int temp;
public String tempName;

public TestData(int temp, String tempName) {
this.temp = temp;
this.tempName = tempName;
}

protected TestData(Parcel in) {
temp = in.readInt();
tempName = in.readString();
}

public static final Creator<TestData> CREATOR = new Creator<TestData>() {
@Override
public TestData createFromParcel(Parcel in) {
return new TestData(in);
}

@Override
public TestData[] newArray(int size) {
return new TestData[size];
}
};

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(temp);
dest.writeString(tempName);
}
}


보이는 바와 같이 int형 temp와 String형 tempName을 담고 있는 TestData라는 객체를 Intent를 통해 전달하기 위해 Parcelable을 구현하는 과정에서 많은 코드량을 생산하였다.


샘플 코드에서는 데이터 클래스에 정의된 필드의 수가 적었지만 실 구현과정에서 많은 수의 필드를 담고 있는 데이터 클래스에대한 Parcelable 구현은 생각하고싶지 않다.



때문에 본 포스트에서는 'Parceler'라는 오픈소스 라이브러리를 이용하여 보다 쉽게 Parcelable를 구현하는 방법에 대해 기술한다.


Parceler란?

Parcelable Class를 보다 쉽게 생성할 수 있도록 지원하는 어노테이션 라이브러리

공식 홈페이지에 간단한 사용법과 설명이 있으니 자세한 내용은 해당 페이지 참조.



해당 페이지 내 가이드 내용을 간략하게 요약하면 다음과 같다.


1. maven이나 gradle에 라이브러리 추가하세요. like this


<dependency>
    <groupId>org.parceler</groupId>
    <artifactId>parceler</artifactId>
    <version>1.1.10</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.parceler</groupId>
    <artifactId>parceler-api</artifactId>
    <version>1.1.10</version>
</dependency>

or Gradle:

compile 'org.parceler:parceler-api:1.1.10'
annotationProcessor 'org.parceler:parceler:1.1.10'



2. 추가했으면 원하는 Data 클래스에 어노테이션을 붙이세요. like this


import org.parceler.Parcel;
import org.parceler.ParcelConstructor;

@Parcel
public class TestData {

public int temp;
public String tempName;

@ParcelConstructor
public TestData(int temp, String tempName) {
this.temp = temp;
this.tempName = tempName;
}
}




3. Bundle 또는 Intent에 데이터 객체 Put 처리 시 Parcels.wrap을 통해 처리하세요. like this


Bundle bundle = new Bundle();
bundle.putParcelable("testData", Parcels.wrap(testData));




4. 꺼낼때는 조심스럽게 Parcels.unwrap하여 꺼내보세요. like this


Object value = Parcels.unwrap(bundle.getParcelable(key));



이러면 아주 쉽게 끝이 나게 된다.


본 포스트에서는 데이터 객체의 Parcelable 처리를 위한 어노테이션 라이브러리를 소개하였다. 해당 라이브러리를 이용하면 누구나 쉽게 Parcelable 처리가 가능하지만 

안드로이드 기본 라이브러리가 아니기 때문에 버그 발생가능성이 높고, 

예상치 못한 기능 미지원이 있을 수 있으니 많은 고민 후 도입해 보기를 바라며 이번 포스트를 마무리하고자 한다.


반응형

+ Recent posts