0

I have 3 classes: MainActivity, homePage and createPage; and a list List<Recipe> recipeList = new ArrayList<>() in MainActivity.

The user enters the homePage from the MainActivity. From homePage, the user can enter createPage and create a new recipe. This new recipe is intended to be passed back to MainActivity.

I've searched online and came across parcels. But when I tried, I get a NullPointerException.

Code for createPage where the list is passed on

ArrayList<Recipe> rList = new ArrayList<>();
Recipe r = new Recipe(...);
rList.add(r)
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);
i.putExtras(b);
i.setClass(createPage.this, homePage.class);
startActivity(i);

Code for homePage where the list is received.

Is there something wrong with the getIntent()? Because when moving from MainActivity to homePage, it doesn't receive a bundle. Is this causing the error?

Intent intent = getIntent();
Bundle b = this.getIntent().getExtras();
if (b != null) {
    Recipe r = b.getParcelable("recipe");
    recipeList.add(r);
}

Code for Recipe class

public class Recipe implements Parcelable {

private String name;
private String description;
private String ingredients;
private int duration;
private String steps;
private int thumbnail;

protected Recipe(Parcel in) {
    name = in.readString();
    description = in.readString();
    ingredients = in.readString();
    duration = in.readInt();
    steps = in.readString();
    thumbnail = in.readInt();
}

public static final Creator<Recipe> CREATOR = new Creator<Recipe>() {
    @Override
    public Recipe createFromParcel(Parcel in) {
        return new Recipe(in);
    }

    @Override
    public Recipe[] newArray(int size) {
        return new Recipe[size];
    }
};

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}

public String getIngredients() {
    return ingredients;
}
public void setIngredients(String ingredients) {
    this.ingredients = ingredients;
}

public int getDuration() {
    return duration;
}
public void setDuration(int duration) {
    this.duration = duration;
}

public String getSteps() { return steps; }
public void setSteps(String steps) { this.steps = steps; }

public int getThumbnail() { return thumbnail; }

public Recipe() {}

public Recipe(String name, int duration, String ingredients, String description, String steps, int thumbnail) {
    this.name = name;
    this.description = description;
    this.ingredients = ingredients;
    this.duration = duration;
    this.steps = steps;
    this.thumbnail = thumbnail;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(name);
    parcel.writeString(description);
    parcel.writeString(ingredients);
    parcel.writeInt(duration);
    parcel.writeString(steps);
    parcel.writeInt(thumbnail);
}

}

Kelven Lim
  • 73
  • 8

3 Answers3

0

Alternatively you can use serializable, that will be less complex.

For reference : https://stackoverflow.com/a/2736612/9502601

Eventhough parcellables are more faster but if you want a less complex solution then you can go for it.

For Comparison between Serializable and Parcelable. https://stackoverflow.com/a/23647471/9502601

Shayan Samad
  • 294
  • 1
  • 10
0

you are writing into Parcelable whole array under "recipe" key

b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);

but on onther side you are looking not for list but for single Recipe item under same key

Recipe r = b.getParcelable("recipe");

you should use getParcelableArrayList or if you have only one Recipe for passing to another Activity just use putParcelable (not list)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Yes, I am indeed passing one Recipe object through everytime a new recipe is created in `createPage`. So instead of passing through the whole list, how would I pass through a single object? If I use Bundle, can I pass through a custom class? – Kelven Lim Jun 20 '22 at 07:07
  • For receiving the Bundle, if I use Intent intent = getIntent(); Bundle b = this.getIntent().getExtras(); List temp = new ArrayList(); if (b != null) { temp = b.getParcelable("recipe"); recipeList.addAll(temp); } would this work? – Kelven Lim Jun 20 '22 at 07:09
  • just use `b.putParcelable("recipe", r);` instead of `putParcelableArrayList` line. receiving code is fine, as it expects single object, not an array – snachmsm Jun 20 '22 at 07:10
  • btw. use some [logging](https://developer.android.com/studio/debug/am-logcat) and [print your `Bundle`](https://stackoverflow.com/questions/5968896/listing-all-extras-of-an-intent) for ensuring that data is in there – snachmsm Jun 20 '22 at 07:12
0

You can use this gson Lib for this

implementation 'com.google.code.gson:gson:2.8.9'

Send Data with Intent

Recipe r = new Recipe(...);
String recipeString = new Gson().toJson(r);
intent.putExtra("recipe",recipeString);

// For ArrayList
ArrayList<Recipe> recipeList = new ArrayList<>();
String recipeString = new Gson().toJson(recipeList);
intent.putExtra("recipeList",recipeString);

Receive Data From Intent

Recipe r = new Gson().fromJson(intent.getStringExtra("recipe"), Recipe.class);

// For Array List
Type listType = new TypeToken<ArrayList<Recipe>>(){}.getType();
ArrayList<Recipe> recipeList = new Gson().fromJson(intent.getStringExtra("recipeList"),listType);
Gulab Sagevadiya
  • 872
  • 1
  • 8
  • 20