Android Parcelable example
안드로이드에서 Parcelable 인터페이스를 이용하여 Student객체를 다른 액티비티에 전달하는 예
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidapp" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Activity02" android:label="두번째 액티비티" > </activity> </application> </manifest>
res/layout/main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rootLayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="onBtnSend" android:text="두번째 액티비티에 객체를 전송하자" /> </LinearLayout>
res/layout/second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="첫번째 액티비티에서 전달된 값은 다음과 같습니다" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="번 호: " /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="이 름: " /> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dip" android:text="전 화: " /> </LinearLayout>
MainActivity.java (Student형 객체를 다른 액티비티로 전달하는 메인 액티비티 )
package com.example.androidapp; import android.os.Bundle; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void onBtnSend(View v) { Intent intent = new Intent(this, Activity02.class); Student st = new Student(10, "장미란", "010-346-7543"); intent.putExtra("student", st); startActivity(intent); } }
Student.java ( 인텐트로 전달될 Object형이므로 Parcel 형으로 전달되어야 하므로 Parcelable 인터페이스를 구현한다 )
package com.example.androidapp; import android.os.Parcel; import android.os.Parcelable; public class Student implements Parcelable { private int id; private String name; private String phone; public Student() {} public Student(int id, String name, String phone) { this.id = id; this.name = name; this.phone = phone; } public Student(Parcel parcel) { this.id = parcel.readInt(); this.name = parcel.readString(); this.phone = parcel.readString(); } public static final Parcelable.Creator<Student> CREATOR = new Parcelable.Creator<Student>() { public Student createFromParcel(Parcel source) { return new Student(source); } public Student[] newArray(int size) { return new Student[size]; } }; public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeString(name); dest.writeString(phone); } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } }
Activity02.java (첫번째 액티비티로부터 Parcel 객체를 받아서 원래의 Student 형으로 변환하는 액티비티)
package com.example.androidapp; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Parcelable; import android.widget.TextView; public class Activity02 extends Activity { TextView tv1, tv2, tv3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second); tv1 = (TextView) findViewById(R.id.textView1); tv2 = (TextView) findViewById(R.id.textView2); tv3 = (TextView) findViewById(R.id.textView3); Intent intent = getIntent(); Parcelable parcel = intent.getExtras().getParcelable("student"); Student st = (Student) parcel; tv1.append(st.getId()+""); tv2.append(st.getName()); tv3.append(st.getPhone()); } }