1

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());
viewRecipe.putExtras(input);
startActivity(viewRecipe);

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

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
RobVoisey
  • 1,083
  • 15
  • 24

5 Answers5

3

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.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • I disagree with this. Read this discussion http://stackoverflow.com/questions/5550670/benefit-of-using-parcelable-instead-of-serializing-object – kosa 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. – kosa 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. – kosa Feb 29 '12 at 15:39
  • Well, I'm pretty sure the performance is bad in both scenarios, but I guess whatever floats your boat. – dmon Feb 29 '12 at 19:37
1

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 :)

user1227313
  • 123
  • 2
  • 2
  • 7
1

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.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • 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"); – RobVoisey Feb 29 '12 at 15:08
  • i have no idea what that means, sorry, im kinda new to android + java – RobVoisey 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. – kosa Feb 29 '12 at 15:13
  • What ever you see the text in red color there while app crashing. – kosa Feb 29 '12 at 15:18
  • other then copying and pasting all of the red text, which just appears to say "at android.app...", ive posted the two which stand out in the OP – RobVoisey Feb 29 '12 at 15:24
  • The issue is you passed input as string recipes.ReturnRecipe(position).toString() and trying to convert it as Recipe currentRecipe = (Recipe) extras.get("myRecipe"); which is trhwoing classcastexception. – kosa Feb 29 '12 at 15:33
  • you may try by removing toString(). Refer this link it may help you http://www.coderanch.com/t/470615/Android/Mobile/Passing-object-one-other-activity – kosa Feb 29 '12 at 15:36
  • so what should i be doing? if i remove the '.toString()' which would then send a serializable object (i think) its still crashing – RobVoisey Feb 29 '12 at 15:36
  • oh, sorry, it does run, except everything is null after i retrieved it, its definately have values passed to it when it goes in – RobVoisey Feb 29 '12 at 15:39
  • seeing as im comparing mine to code that works perfectly and mine doesnt work, can you suggest some alternate methods? it'll be helpful if i could also use it with a SQL database for later – RobVoisey Feb 29 '12 at 15:50
  • Unfortunately if you want to pass objects between intents there is no alternative. You should make this work otherwise you may need to re-design your project. If you don't want to pass whole recipe class, then it is easy. So, it is your call. – kosa Feb 29 '12 at 15:55
  • thank you, i'll have a think about it and try to write some code outside of the project - you've been a great help, thanks :D – RobVoisey Feb 29 '12 at 16:00
0

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."

hurtledown
  • 673
  • 6
  • 18
  • problem is its crashing when i try to retrieve the object from the second activity – RobVoisey 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
0

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.

Paul Grime
  • 14,970
  • 4
  • 36
  • 58