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?
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);
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.
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() .
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.
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