Take the 2-minute tour ×

What I'm trying to do is send an object through Intents; I've been looking at implementing Serializable like so:

Intent viewRecipe = new Intent(t, RecipeView.class);
Bundle input = new Bundle();
input.putSerializable("myRecipe", recipes.ReturnRecipe(position).toString());

However, I'm guessing because I have a complex class it isn't working. Can anyone help explain how i would be able to convert this object to a Serialized string? Also it would be helpful if you know of an easier/simpler way of doing it; may also be worth noting I plan to integrate it with a SQL database further down the line, but I'm stuck at the moment.

The class is:

public class Recipe implements Serializable {

int mRating;
String mName;
String mDescription;

ArrayList<Ingredient> ingredients;
ArrayList<Step> instructions;

I'm not sure if i have to override "readObject" and "writeObject", or even how I would start to do that. As a side note, Ingredient and Step are not complex classes:

public class Ingredient {

String mName;
int mValue;
String mMeasurement;

public class Step {

int mIndex;
String task;
Date time;

To cut a long story short, I have no idea what I'm doing and could use some guidence. I've read a fair bit on how to serialize simple classes but they haven't really helped.

EDIT: Including the error im getting, just imeplementing Serilizable gets me this error when i retrieve the object from the second acitivity.

Intent myIntent = getIntent();
    Bundle extras = myIntent.getExtras(); //breaks on this line

    currentRecipe = (Recipe) extras.get("myRecipe");

02-29 15:15:56.521: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pocket.recipes/com.pocket.recipes.RecipeView}: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Above is the line shown after "FATAL EXCEPTION: main"

I then have a lot of errors merely saying "at android.app...."

I also have this line in there:

02-29 15:15:56.521: E/AndroidRuntime(534): Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to com.pocket.recipes.Recipe

Sorry about this, I'm new to Java / Android

share|improve this question

5 Answers 5

up vote 1 down vote accepted

You don't need to override methods. Implementing interface should be enough. You haven't specified exactly what is not working, but complexity shouldn't be an issue as long as object you are trying to serialize has data which is serializable.

share|improve this answer
well, it crashes when i try to retrieve the object using : Intent myIntent = getIntent(); Bundle extras = myIntent.getExtras(); //currentRecipe.writeObject() currentRecipe = (Recipe) extras.get("myRecipe"); –  Tsar Feb 29 '12 at 15:08
post stack trace –  Nambari Feb 29 '12 at 15:10
i have no idea what that means, sorry, im kinda new to android + java –  Tsar Feb 29 '12 at 15:12
Assuming you are using eclipse, Window > Show View > Other-->Then expand Android and then select LogCat, here you will see error stack. Post it in your question. –  Nambari Feb 29 '12 at 15:13
added the error, is this what you meant? –  Tsar Feb 29 '12 at 15:18

Unless you can't avoid it, you shouldn't use Serializable when sending data via Intents (due to performance concerns). You should implement the Parcelable interface instead. There's a lot of cruft, but it's actually pretty simple, unless you have an extremely complex class. You can also implement Parcelable on the child classes, and then just use writeParcelableArray() for your lists of ingredients and steps.

share|improve this answer
I disagree with this. Read this discussion stackoverflow.com/questions/5550670/… –  Nambari Feb 29 '12 at 15:05
So ... the accepted answer states the same thing I said, that performance on serializable sucks and that you shouldn't use it. –  dmon Feb 29 '12 at 15:13
that part I agree, but Parcelable usage is for inter-process communication (or) between services, not for intent to intent. Read second paragraph. OP trying to pass data between Intent. –  Nambari Feb 29 '12 at 15:14
Right, but they provide no alternative other than "use a bundle", which technically you would be using since you put your parceled object in a Bundle. –  dmon Feb 29 '12 at 15:17
It is not about using Bundle. It is about Parcelable or Serializable, one is good for Services another is good for activities/intents. –  Nambari Feb 29 '12 at 15:39

I had the similar problem. After a long and tedious debugging, I realized that all the classes whose instances are used as member variables, are to be serialized. So, in your case, class Ingredient and class Step also need to be serialized, along with the class Recipe. I hope this helps :)

share|improve this answer

You don't need to implement anything. Just declaring the "implement Serializable" make the instances of the class serializable. as written in the documentation (http://developer.android.com/reference/java/io/Serializable.html) "Implementing this interface is enough to make most classes serializable."

share|improve this answer
problem is its crashing when i try to retrieve the object from the second activity –  Tsar Feb 29 '12 at 15:11
Probably it's because you have to make sure that not only the main class needs to be Serializable, but also all the classes inside it: if you have Recipe that contains references to instances of the Ingredient class, in order to serialize Recipe, you also have to declare Serializable the Ingredient class. –  hurtledown Feb 29 '12 at 16:03

It depends on the context of the application, I think. Parcelable is one option, but other apps implement a ContentProvider and pass a URL or an id in the Intent, which are then used to lookup the item in the ContentProvider.

share|improve this answer

Your Answer


