How do I pass custom objects between activites in android? I'm aware of bundles but I can't seem to see any functionality for this in them. Could anyone show me a nice example of this?
5 Answers
You should implement Parcelable interface.
Link to documentation.

- 2,358
- 3
- 27
- 63
-
3Why is it not possible to send objects in bundle? I mean, why is Android not supporting it? All the activities run in same process so isn't it easy to support it? – Sudarshan Bhat Aug 03 '12 at 04:57
-
2@Enigma I think I get your question. If you mean "pass reference to the object" in bundle, then it is a bad idea, because Activity which created the object can be destroyed due to low memory and the object will be lost. – Aug 04 '12 at 07:57
-
if there is a reference to an object, it will not get destroyed! i think there should be something more technical than Garbage Collection stuff, about this! – Ehsan Mirsaeedi Aug 24 '14 at 10:33
-
@EhsanMirsaeedi if your reference is preventing enclosing object from being garbage collected, then you have a memory leak. – Aug 25 '14 at 10:45
-
@Igor Filippov Link is death – Sven van den Boogaart Aug 08 '15 at 15:09
-
@SvenB I guess Android developer documentation explains it good enough. – Aug 08 '15 at 15:25
-
The first link is broken. – MattCochrane Nov 04 '16 at 21:12
Using Parcelable interface you can pass custom java object into the intent.
1) implement the Parcelable interface to your class like:
class Employee implements Parcelable
{
}
2) Pass the Parcelable object into the intent like:
Employee mEmployee =new Employee();
Intent mIntent = new Intent(mContect,Abc.class);
mIntent.putExtra("employee", mEmployee);
startActivity(mIntent);
3) Get the data into the new [Abc] Activity like:
Intent mIntent = getIntent();
Employee mEmployee = (Employee )mIntent.getParcelableExtra("employee");

- 1,894
- 3
- 22
- 28
a Parcel
MIGHT solve your problem.
think of a Parcel
as an "array" (metaphorical) of primitive types (long, String, Double, int, etc). if your custom class is composed of primitive types ONLY, then change your class declaration including implements Parcelable
.
you can pass a parcelable object thru an intent with no difficulty whatsoever (just like you would send a primitive-typed object). in this case i have a parcelable custom class called FarmData (composed of longs, strings and doubles) which i pass from one activity to another via intent.
FarmData farmData = new FarmData();
// code that populates farmData - etc etc etc
Intent intent00 = new Intent(getApplicationContext(), com.example.yourpackage.yourclass.class);
intent00.putExtra("farmData",farmData);
startActivity(intent00);
but retrieving it may be tricky. the activity that receives the intent will check if a bundle of extras was send along with the intent.
Bundle extras = getIntent().getExtras();
FarmData farmData = new FarmData();
Intent intentIncoming = getIntent();
if(extras != null) {
farmData = (FarmData) intentIncoming.getParcelableExtra("farmData");// OK
}

- 9,424
- 6
- 76
- 100
Given an object PasswordState that implements Serializable throughout the object tree, you can pass this object to anther activity as in:
private void launchManagePassword() {
Intent i= new Intent(this, ManagePassword.class); // no param constructor
PasswordState outState= new PasswordState(lengthKey,timeExpire,isValidKey,timeoutType,"",model.getIsHashPassword());
Bundle b= new Bundle();
b.putSerializable("jalcomputing.confusetext.PasswordState", outState);
i.putExtras(b);
startActivityForResult(i,REQUEST_MANAGE_PASSWORD); // used for callback
}

- 3,319
- 2
- 20
- 17
One simple way to pass an object between activities or make a object common for all applicattion, is create a class extending Application.
Here is a example:
public class DadosComuns extends Application{
private String nomeUsuario="";
public String getNomeUsuario() {
return nomeUsuario;
}
public void setNomeUsuario(String str) {
nomeUsuario = str;
}
}
In all your others activities, you just need instantiate one object "DadosComuns", declarating as a Global Variable.
private DadosComuns dadosComuns;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//dados comuns
dadosComuns = ((DadosComuns)getApplicationContext());
dadosComuns.setNomeUsuario("userNameTest"); }
All others activities that you instantiate dadosComuns = ((DadosComuns)getApplicationContext()); you can acess getNomeUsuario() == "userNameTest"
In your AndroidManifest.xml you need to have
<application
android:name=".DadosComuns"

- 1,226
- 11
- 13