Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android nested object parceling

Tags:

android

parcel

how can we parcel nested object in an intent?.
For example lets say there is an object A which contain one string variable and one another object B. now B contain object C . C contain a list of string. so how i can parcel object A in an intent.
any help on this will be appreciated.
thanks in advance.

Implementation:

public static class A 
{
    private B  ObjectB;
}

public static class B 
{
    private String type;
    private List<C> C;
}

public static class C 
{
    private List<D> D;
}

public static class D 
{
    private String id;
    private String name;
    private String address;
    private String email;
}

how to write parcelable for class C. i am using

dest.writeParceable(ObjectC,flag)

For reading:

in.readParcelable(C.getClass().getClassLoader());

but its not working


1 Answers

You would have to implement parcelable for object B and then implement parcelable for object A. I have never done this but the above should work.

Edit

See the code snippets below which illustrates how to implement parcelable for Class C.

  1. First implement parcelable for Class D like so:

    dest.writeString(id); 
    dest.writeString(name);  
    dest.writeString(address);  
    dest.writeString(email);
    
    id = in.readString();  
    name = in.readString();  
    address = in.readString();  
    email = in.readString();  
    
  2. Then implement parcelable for Class C as follows:

    dest.writeList(D);
    
    in.readList(D,this.getClass().getClassLoader());
    

This is untested code as I have never implemented nested parceling but worth a try. Hope that helps.

like image 185
Mandel Avatar answered Jul 22 '26 11:07

Mandel