Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How to send an object from one Android Activity to another using Intents?

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?
by

2 Answers

espadacoder11
You can send serializable object through intent

// send where details is object
ClassName details = new ClassName();
Intent i = new Intent(context, EditActivity.class);
i.putExtra("Editing", details);
startActivity(i);


//receive
ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");

And

Class ClassName implements Serializable {
}
pankajshivnani123
Your class should implement Serializable or Parcelable.

public class MY_CLASS implements Serializable

Once done you can send an object on putExtra

intent.putExtra("KEY", MY_CLASS_instance);

startActivity(intent);

To get extras you only have to do

Intent intent = getIntent();
MY_CLASS class = (MY_CLASS) intent.getExtras().getSerializable("KEY");

If your class implements Parcelable use next

MY_CLASS class = (MY_CLASS) intent.getExtras().getParcelable("KEY");

Login / Signup to Answer the Question.