Parcelable(SDK)

Interface for classes whose instances can be written to and restored from aParcel.

Classes implementing the Parcelable interface must also have a static field calledCREATOR, which is an object implementing theParcelable.Creatorinterface.


Passing data between activities is quite easy.

You would normally do that using the Bundle packed into an intent. But sometimes you need to pass complex objects from one activity to another.

One workaround would be to keep a static instance of the object int your Activity and access it from you new Activity. This might help, but it's definitely not a good way to do this.

To pass such objects directly through the Bundle, your class would need to implement the Parcelable interface.


Example:

MainActivity, SubActivity, Person


MainActivity

package com.learn;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener {private static final String TAG = "Parcelable";public static final String KEY = "key";private Button btn;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        Log.d(TAG, "MainActivity");                btn = (Button)findViewById(R.id.btn);    btn.setOnClickListener(this);        }            @Override    public void onClick(View v) {        Log.d(TAG, "onClick");            if(R.id.btn == v.getId()){        Person mPerson = new Person();    mPerson.setName("Bill");    mPerson.setAge(22);        Bundle bundle = new Bundle();    bundle.putParcelable(KEY, mPerson);// mPerson -> Parcelable            Log.d(TAG, "mPerson=" + mPerson);        Intent intent = new Intent(this, SubActivity.class);    intent.putExtras(bundle);    startActivity(intent);            Log.d(TAG, "startActivity");    }        }}

SubActivity

package com.learn;import android.app.Activity;import android.os.Bundle;import android.os.Parcelable;import android.util.Log;import android.widget.TextView;public class SubActivity extends Activity {private static final String TAG = "Parcelable";private TextView tv;@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main);        Log.d(TAG, "SubActivity");        Parcelable parcelable = getIntent().getParcelableExtra(MainActivity.KEY);Person mPerson = (Person)parcelable;// Parcelable -> Person        Log.d(TAG, "parcelable=" + parcelable + "; mPerson=" + mPerson);        tv = (TextView)findViewById(R.id.tv);tv.setText("name=" + mPerson.getName() + "; age=" + mPerson.getAge());}}

Person

package com.learn;import android.os.Parcel;import android.os.Parcelable;import android.util.Log;public class Person implements Parcelable{private static final String TAG = "Parcelable";private String name;private int age;public Person(){}public void setName(String name){this.name = name;}public String getName(){return this.name;}public void setAge(int age){this.age = age;}public int getAge(){return this.age;}public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {@Overridepublic Person createFromParcel(Parcel source) {            Log.d(TAG, "createFromParcel(Parcel source)");            Person mPerson = new Person();mPerson.name = source.readString();mPerson.age = source.readInt();return mPerson;}@Overridepublic Person[] newArray(int size) {            Log.d(TAG, "newArray(int size)");            return new Person[size];}};@Overridepublic int describeContents() {        Log.d(TAG, "describeContents()");        return 0;}@Overridepublic void writeToParcel(Parcel dest, int flags) {        Log.d(TAG, "writeToParcel(Parcel dest, int flags)");        dest.writeString(name);dest.writeInt(age);}}

main.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent">    <TextView  android:id="@+id/tv"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello" />        <Button android:id="@+id/btn"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="Parcelable" />    </LinearLayout>

Running Result

Android Parcelable_第1张图片


click, then

Android Parcelable_第2张图片


Log

D/Parcelable(  487): MainActivityD/Parcelable(  487): onClickD/Parcelable(  487): [email protected]D/Parcelable(  487): writeToParcel(Parcel dest, int flags)D/Parcelable(  487): startActivityD/Parcelable(  487): SubActivityD/Parcelable(  487): createFromParcel(Parcel source)D/Parcelable(  487): [email protected]; [email protected]


Analyse

writeToParcel(Parcel dest, int flags) ---- bundle.putParcelable(KEY, mPerson)

createFromParcel(Parcel source) ---- (Person)getIntent().getParcelableExtra(MainActivity.KEY)

source ---- dest


Download


Parcelable - How to do that in Android

Writing Parcelable classes for Android

更多相关文章

  1. android 双击图片放大缩小
  2. android加载本地图片
  3. Android 从网络加载图片
  4. Android从相机或相册获取图片裁剪
  5. android camera2 api点击图片实现聚焦
  6. android 读取sd卡中的图片
  7. Android drawableleft如何设置图片大小
  8. android自带图片资源图标一览,android.R.drawable
  9. ImageView设置图片大小

随机推荐

  1. google手机Android编程语言-Simple语言定
  2. Android(安卓)四大组件 ————Service(
  3. Android(安卓)面试文档分享
  4. Android中使用Retrofit刷新Token
  5. 4款最好的Android设备HTML编辑器
  6. [转]android解决apk编译方法数超过64k的
  7. Android:native和上层framework基于socke
  8. H264解码器源码(Android(安卓)1.6 版)
  9. Android查看数据库方法及工具
  10. 线程方法Android:异步调用详解