FrankBr
FrankBr

Reputation: 896

getParcelable returns null even if passed object is no null

I want to pass a parcelable object to an activity to another one. So I implemented a parcelable class to accomplish this. And I coded:

Intent intent = new Intent(mainactivity.this, SecondActivity.class);
Object[] object = new Object();
intent.putExtra("Object ", object);
startActivity(intent);

and in the second activity I coded:

Object[] object = (Object[]) getIntent().getExtras().getParcelable("object");

When I pass the object to Intent, it's not null. While, when I get it in the second Activity is null. Do you have any suggestions? Thanks in advance!

Upvotes: 3

Views: 6290

Answers (2)

FrancescoAzzola
FrancescoAzzola

Reputation: 2654

You can simply use in the caller:

Intent i = new Intent(EditActivity.this, ViewActivity.class);
i.putExtra("myObj", p);
startActivity(i);

In the receiver:

Bundle b = i.getExtras();
Person p = (Person) b.getParcelable("myObject");

Hope this help you

Upvotes: 2

waqaslam
waqaslam

Reputation: 68187

Change this:

Object object = (Object) getIntent().getExtras().getParcelable("object");

To this:

Object object = getIntent().getExtras().get("Object");

getParcelable should only be used if you are using putParcelable or inserting a Parcelable object using putExtra in the sending part of code.

P.S. also mind the difference of key-name Object and object

Upvotes: 8

Related Questions