Parcelable in Android

Parcelable(SDK)

Interface for classes whose instances can be written to and restored from a Parcel

Classes implementing the Parcelable interface must also have a static field called CREATOR, which is an object implementing the Parcelable.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


SubActivity


Person


main.xml


Running Result


click, then



Log


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

posted @ 2011-11-08 20:25  张长胜  阅读(163)  评论(0编辑  收藏  举报