Android

Intent로 객체전달

tomato13 2011. 3. 16. 08:45

객체는 Parcelable을 상속받아야 하고 기본적으로 아래와 같이 몇개의 함수에 대한 overriding작업 및 멤버변수선언이 필요하다.


import android.os.Parcel;

import android.os.Parcelable;


public class MyParcel implements Parcelable {

public String mName = null;

public String mAge = null;


public static final Parcelable.Creator<MyParcel> CREATOR = new Parcelable.Creator<MyParcel>() {

public MyParcel createFromParcel(Parcel in) {

return new MyParcel(in);

}


public MyParcel[] newArray(int size) {

return new MyParcel[size];

}

};


public MyParcel() {

mName = "Mr.Moon";

mAge = "19";

}


public MyParcel(Parcel in) {

mName = in.readString();        // 객체의 멤버변수에 값을 할당받는다.

mAge = in.readString();

}


public int describeContents() {

// TODO Auto-generated method stub

return 0;

}


public void writeToParcel(android.os.Parcel out, int arg1) {

// TODO Auto-generated method stub

out.writeString(mName);                   // 객체의 보내고자 하는 멤버변수를 써준다.

out.writeString(mAge);

}

}


* 보내는 코드예제

Intent intent = new Intent(this, IntentCaller.class);

MyParcel l_parcel = new MyParcel();

l_parcel.mName = nameText.getText().toString();

l_parcel.mAge = ageText.getText().toString();

intent.putExtra("parcel", l_parcel);


mInit = false;

startActivity(intent);


* 받는 코드 예제

MyParcel l_parcel = getIntent().getExtras().getParcelable("parcel");

'Android' 카테고리의 다른 글

RGB hexadecimal code  (0) 2011.04.15
ISO 3166-2   (0) 2011.04.07
monkey test  (0) 2011.01.28
Android에서 텍스트 파일 로딩하기  (0) 2010.12.24
logcat filtering  (0) 2010.11.09