Is it possible to to put an object of type Object into an intent as a Extra? I have a variable of type object and won't know until it is assigned a value as to what the object datatype is. Maybe something to do with serialization or as a bundle i'm not sure? And then in the next activity how do I then get this value in order to store it in an ArrayList<Object>
?
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. getSerializableExtra .
To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.
Bundle
by way of Intent#putExtra
does not have any function to add an Object
. You can only pass in a Parcelable
or a Serializable
object. Any object you want to pass via an Intent
must implement one of those interfaces. It's recommended to implement Parcelable
there is a brief guide here: Pass by value Parameters using Parcelables.
Also this question has more helpful answers: How to send an object from one Android Activity to another using Intents?
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