I have a fragment where on clicking 'Next' I serialize an arraylist of object 'DishItem' and pass it via the MainActivity to the subsequent fragment. When I tap the home button on the device and close the app, it crashes with the following error:
Process: com.edgeshipments.logistics, PID: 21555
java.lang.RuntimeException: Parcel: unable to marshal value com.edgeshipments.logistics.Menu.DishItem@799475b
at android.os.Parcel.writeValue(Parcel.java:1885)
at android.os.Parcel.writeList(Parcel.java:1092)
at android.os.Parcel.writeValue(Parcel.java:1832)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:975)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.os.Parcel.writeBundle(Parcel.java:1044)
at androidx.fragment.app.FragmentState.writeToParcel(FragmentState.java:167)
at android.os.Parcel.writeTypedObject(Parcel.java:1737)
at android.os.Parcel.writeTypedList(Parcel.java:1616)
at android.os.Parcel.writeTypedList(Parcel.java:1573)
at androidx.fragment.app.FragmentManagerState.writeToParcel(FragmentManagerState.java:51)
at android.os.Parcel.writeParcelable(Parcel.java:1904)
at android.os.Parcel.writeValue(Parcel.java:1810)
at android.os.Parcel.writeArrayMapInternal(Parcel.java:975)
at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1620)
at android.os.Bundle.writeToParcel(Bundle.java:1303)
at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4389)
at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
This is my model class:
public class DishItem implements Comparable {
private String menuItemDescription, menuItemName;
private double deliveryPrice, pickupPrice;
private int levelCode;
public DishItem() {
}
public DishItem(String menuItemDescription, String menuItemName, int levelCode, double deliveryPrice, double pickupPrice) {
this.menuItemDescription = menuItemDescription;
this.menuItemName = menuItemName;
this.levelCode = levelCode;
this.deliveryPrice = deliveryPrice;
this.pickupPrice = pickupPrice;
}
public String getMenuItemDescription() {
return menuItemDescription;
}
public String getMenuItemName() {
return menuItemName;
}
public int getLevelCode() {
return levelCode;
}
public double getDeliveryPrice() {
return deliveryPrice;
}
public double getPickupPrice() {
return pickupPrice;
}
@Override
public int compareTo(Object o) {
int compareLevel = ((DishItem)o).getLevelCode();
return this.levelCode - compareLevel;
}
}
This is how I add the arraylist of objects into a bundle in the first fragment:
bundle.putSerializable("restaurantMenu", dishItems);
And this is how I unwrap the bundle to retrieve the array of objects:
dishItemArrayList = (ArrayList<DishItem>) bundle.getSerializable(restaurantMenu);
What's strange is that I can replace the 2nd fragment with any other random fragment and I get the same problem and yet app only crashes when I close the app from the 2nd fragment. So clearly this is happening when I communicate via the MainActivity. And this is all I am doing there when implementing the interface between the fragments:
@Override
public void onRestaurantMap(Bundle bundle) {
boolean direction = bundle.getBoolean("direction");
if (direction) {
bundle.clear();
bundle = new Bundle();
MenuCardFragment fragment = new MenuCardFragment();
openFragment(bundle, fragment, true);
} else {
CreateFragment fragment = new CreateFragment();
openFragment(bundle, fragment, false);
}
}