Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

Why bundle has getParcelableArrayList, getParcelable methods; but Intent has only putParcelableArrayListExtra method? Can I transmit only object<T>, not ArrayList of one element? Then, what is getParcelable for?

like image 478
yital9 Avatar asked Apr 11 '12 13:04

yital9


People also ask

How do I send a Parcelable intent?

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity: Intent intent = new Intent(getBaseContext(), NextActivity. class); Foo foo = new Foo(); intent. putExtra("foo ", foo); startActivity(intent);

Can we pass object through intent in Android?

One way to pass objects in Intents is for the object's class to implement Serializable. This interface doesn't require you to implement any methods; simply adding implements Serializable should be enough. To get the object back from the Intent, just call intent.

What is the difference between an intent object and a bundle object?

An Intent is also capable of holding data in the form of name-value pairs (via putExtra() ). But while overriding the onCreate() method we pass a Bundle as the parameter, which ultimately also holds values in the form of name-value pairs and is able to store information with the help of onSaveInstanceState() .


1 Answers

Intent provides bunch of overloading putExtra() methods.

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:

Intent intent = new Intent(getBaseContext(), NextActivity.class); Foo foo = new Foo(); intent.putExtra("foo ", foo); startActivity(intent); 

To get it from intent in another activity:

Foo foo = getIntent().getExtras().getParcelable("foo"); 

Hope this helps.

like image 56
yorkw Avatar answered Oct 14 '22 10:10

yorkw