Considering the Android architecture and considering the operative design of Android with things like intents, views, activity, content provider, and so on, can someone explain to me what kind of "thing" is a parcelable or a bundle ? The explanation written on the Android website sounds a little bit too lame for me, i mean reading this "A special type-safe container, called Bundle, is available for key/value maps of heterogeneous values." i know nothing more about Bundles, to me they can be XML files, hash maps, and all the other variant for a "key/value maps".
What is a Parcelable or a Bundle and what is their desing and what they do?
Thanks.
A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.
Parcelable is a serialization mechanism provided by Android to pass complex data from one activity to another activity.In order to write an object to a Parcel, that object should implement the interface “Parcelable“.
The Parcelable interface adds methods to all classes you want to be able to transfer between activities. These methods are how parcelable deconstructs the object in one activity and reconstructs it in another. For this example you'll look at how to implement parcelable in a simple class.
Bundle is a container for named values of types standard for android (including Parcelable ), which is used to pass data between activies, fragments and other android app entites.
Parcelable
and Bundle
are packages of information that you want to send with the intent!
Bundle
: If you want to start new activity
you can send Bundle
of information to the activity
along with the new Intent
that you created:
// Bundle b is sent with new intent i
Bundle b = new Bundle();
b.putString(key, value);
b.putInt(key, value);
Intent i = new Intent(...);
i.putExtras(b);
startActivity(i);
// In the activity which started from the intent i, you can get the bundle b
this.getIntent().getExtras();
Parcelable
is a interface
, if you want to pass an object
(your own class) with bundle
or with intent
, you should implement this interface
:
class Example implements Parcelable{
// some information here
}
// You can send with intent or bundle:
b.putParcelable(key, value);
i.putExtra(name, value);
More detail of google android here: Bundle Parcelable
Android has defined a new light weight IPC (Inter Process Communication) data structure called Parcel, where you can flatten your objects in byte stream, same as J2SDK’s Serialization concept.
A short definition of an Android Parcel would be that of a message container for lightweight, high-performance Inter-process communication (IPC). On Android, a "process" is a standard Linux one, and one process cannot normally access the memory of another process, so with Parcels, the Android system decomposes objects into primitives that can be marshaled/unmarshaled across process boundaries.
But Parcels can also be used within the same process, to pass data across different components of a same application. As an example, a typical Android application has several screens, called "Activities" , and needs to communicate data or action from one Activity to the next. To write an object than can be passed through, we can implement the Parcelable interface. Android itself provides a built-in Parcelable object called an Intent which is used to pass information from one component to another
Bundle is android's way of passing information among android components. As you said its a dictionary where we can put key value pairs. Any thing which can be put in a bundle should be primitive or Parcel.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With