Search code examples
javaandroidparcelableparceler

Parceler error passing a Bundle from one Activity to another


I decided to use Parceler because it looks like a great library and it is very well supported by the author. I am using Parceler to wrap an object and pass it to another Activity in a Bundle. When I attempt to unwrap the object I am getting the error: android.os.Bundle cannot be cast to org.parceler.ParcelWrapper

My FirstActivity code:

 User user = responseData.getUser();
    Bundle bundle = new Bundle();
    bundle.putParcelable("User", Parcels.wrap(user));
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("User", bundle);
    startActivity(intent);

My SecondActivity code:

User user = Parcels.unwrap(this.getIntent().getParcelableExtra("User"));

I suspect this is just a newbie mistake. Any constructive assistance is appreciated!


Solution

  • You just need put wrapped object as argument for putExtra, not Bundle. Here is solution:

    User user = responseData.getUser();
    Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
    intent.putExtra("User", Parcels.wrap(user));
    startActivity(intent);
    

    On SecondActivity, in its onCreate() method do:

    User user = (User) Parcels.unwrap(getIntent().getParcelableExtra("User"));