Parcel and Parcelables

 In Android, parceling is a way of sharing objects between different processes by flattering an object into a String of data called parcel. 





A complex object can be stored into the parcel and then recreated from the parcel by implementing the parcelable interface and then become parcelable objects



Each value in the object is written in sequence to the parcel


The object is recreated by reading data from the parcel in the same order it was written to populate data in a new object. 

Using a arcel to share an object between processes, is functionally similar to using XML or JSON to share data between web services and clients.


A bundle is a parcelable object that contains a key value store of parcelable objects . We use bundles as the argument property in fragments, primarly because of the way that Android lifecycle works.



Activities will be destroyed with a SaveInstanceState if the app is killed when running in the background. All of the information in the SaveInstanceState has to be from parcelables since the state is used to recreate objects when the app gets restarted and is therefore a new process.



When an activity is recreated in this state, the fragment manager needs to be able to recreate all of the fragments. Since they are parcelable, bundles can be stored in the SaveInstanceState, allowing fragments to preserve their argumnts when the process is destroyed and the fragment is recreated.

Previously, we've passed simple objects such as integers that are automatically parceled

Since our app needs to pass a MarsProperty object from the overviewFragment to the detailFragment. Let's see how can we make our MarsProperty parcelable



Exercise




More Info about Parcelables

Comments

Popular posts from this blog

Summary of Lifecycle State and Activity Lifecycle