Programming/안드로이드
[안드로이드] Activity 간 객체 전달 방법(Parcelable)
껑이
2010. 9. 8. 18:53
Activity 간 객체 전달 방법
Parcel을 이용한 객체를 전달하는 방법을 통해
1. 전달하고자 하는 객체 클래스를 Parcelable 인터페이스를 구현한다.
import android.os.Parcel; import android.os.Parcelable; public class CategoryItem implements Parcelable { public String URL; public String Subject; @Override public int describeContents() { // TODO Auto-generated method stub return 0; } @Override public void writeToParcel(Parcel arg0, int arg1) { // TODO Auto-generated method stub arg0.writeString(URL); arg0.writeString(Subject); } public static final Parcelable.CreatorCREATOR = new Creator () { public CategoryItem createFromParcel(Parcel source) { CategoryItem category = new CategoryItem(); category.URL = source.readString(); category.Subject = source.readString(); return category; } public CategoryItem[] newArray(int size) { return new CategoryItem[size]; } }; }
전달시
Intent intent = new Intent(this, AllCategoryActivity.class); intent.putParcelableArrayListExtra("GetCategoryItem", mAllCategory); startActivity(intent);사용시
Bundle bundle = getIntent().getExtras(); mAllCategory = bundle.getParcelableArrayList("GetCategoryItem");