Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Android mean with "all parcelable data types" and how i can use this data?

Tags:

android

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.

like image 962
user827992 Avatar asked Aug 01 '12 01:08

user827992


People also ask

What does Parcelable mean Android?

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.

What is Parcelable data?

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“.

What is a primary purpose of the Parcelable interface?

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.

Are bundles Parcelable?

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.


2 Answers

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

like image 161
Kingfisher Phuoc Avatar answered Oct 29 '22 17:10

Kingfisher Phuoc


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.

like image 24
san Avatar answered Oct 29 '22 16:10

san