3

I want to send my data from Activity1 to Activity2 with putExtra.

My Custom object implements Serializable :

public class ARObjectCategory implements Serializable {

private static final long serialVersionUID = 3128594851129501738L;

public int id;
public String name;
public transient ARObjectCategory parent;
public transient SparseArray<ARObjectCategory> children;
public transient Bitmap iconBitmap = null;
public String icon;
private boolean active = false;

public ARObjectCategory(int id, String name, ARObjectCategory parent) {
    this.id = id;
    this.name = name;
    this.parent = parent;
    this.children = new SparseArray<>();
}


public void addChild(ARObjectCategory child) {
    children.append(child.id, child);
    if (getActive())
        child.setActive(true);
}

public final ARObjectCategory getChild(int index) {
    return children.valueAt(index);
}

public final SparseArray<ARObjectCategory> getChildren() {
    return this.children;
}

public final int getParentLast() {

    ARObjectCategory parentTemp = this.parent;
    while (parentTemp.parent != null) {
        parentTemp = parentTemp.parent;
    }
    return parentTemp.id;
}


public final ARObjectCategory getChildById(int id) {
    return children.get(id, null);
}

public final int getChildrenCount() {
    return children.size();
}

public Boolean getActive() {
    return this.active;
}

public void setActive(Boolean bool) {
    this.active = bool;
}

public Bitmap getIconBitmap() {
    return iconBitmap;
}

public void setIconBitmap(Bitmap iconBitmap) {
    this.iconBitmap = iconBitmap;
}

public String getIcon() {
    return icon;
}

public void setIcon(String icon) {
    this.icon = icon;
}

}

Activity1.java (Sender):

 ARObjectCategory test1= adapter.getItem(position);
       Intent subCat= new Intent(getActivity(), SubCategoriesActivity.class);
        subCat.putExtra("test",test1);
        subCat.putExtra("selected",position);
       startActivity(subCat);

On Debug Mode my object looks ok. SpraseArray got object of AROjcectCategory.

enter image description here

Activity2.java (Reciever):

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        int selected = extras.getInt("selected", -1);
        ARObjectCategory list = (ARObjectCategory) extras.getSerializable("test");

    SparseArray<ARObjectCategory> lista = list.getChildren();
  }

But when i debug the Activity2. I see that my SpraseArray of children is null

enter image description here

Finally when i remove the transient from my objects.

public transient ARObjectCategory parent;
public transient SparseArray<ARObjectCategory> children;

I am getting the following error on my logcat :

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.ethos.ar.core.ARObjectCategory) ........
Caused by: java.io.NotSerializableException: android.util.SparseArray ......

What is the correct way to send Object that has SpraseArray to other Activity.

Tip: Parcelable is not working:

dest.writeParcelable(this.children);
SparseArray<ARObjectCategory> cannot be converted to SparseArray<Object>

Thanks

1 Answer 1

2

When you use the transient modifier, that member is excluded from the serialization process. That's the reason you are getting a null object. When you remove the transient modifier then you receive an exception, but why? That's because the "SparseArray" object itself doesn't implement the "Serializable" interface. Remember that in order to an object be serializable then all of its attributes must implement the "Serializable" interface. Now what can you do? You should implement the "Parcelable" interface of Android, but how? If you have problems using the "SparseArray" in the parcelable process then you can parcel it to another object and the recreate the original "SparseArray" object in the creation process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.