Error in passing data between activites android while using a custom object

460 Views Asked by At

when I pass this object with intent, its not starting the activity.

Custom object code

public class UserVo implements Serializable {

String name,id;
String can_reply,can_view;

public String getName() {
    return name;
}

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

public String getId() {
    return id;
}

public String getCan_reply() {
    return can_reply;
}

public void setCan_reply(String can_reply) {
    this.can_reply = can_reply;
}

public String getCan_view() {
    return can_view;
}

public void setCan_view(String can_view) {
    this.can_view = can_view;
}

public void setId(String id) {
    this.id = id;
}

Sending data through the intent

   intent.putExtra("UserVo",vo);
   intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
     startActivity(intent);
        finish();

Receiving data

    intent=getIntent();
    UserVo vo= (UserVo) intent.getSerializableExtra("UserVo");

I also tried bundle, but it still does not work.

4

There are 4 best solutions below

0
On BEST ANSWER

Look at this link Serializable:

Every serializable class is assigned a version identifier called a serialVersionUID. By default, this identifier is computed by hashing the class declaration and its members. This identifier is included in the serialized form so that version conflicts can be detected during deserialization. If the local serialVersionUID differs from the serialVersionUID in the serialized data, deserialization will fail with an InvalidClassException.

To avoid it:

You can avoid this failure by declaring an explicit serialVersionUID. Declaring an explicit serialVersionUID tells the serialization mechanism that the class is forward and backward compatible with all versions that share that serialVersionUID. Declaring a serialVersionUID looks like this: private static final long serialVersionUID = 0L;

and remember that:

If you declare a serialVersionUID, you should increment it each time your class changes incompatibly with the previous version. Typically this is when you add, change or remove a non-transient field.

For best practise, I recommend you to use Parcelable. It's harder for coding but better for performance.

5
On

If for some reason, you can't make your object a Parcelable, try adding a serialVersionUID. You can generate it through your IDE or choose one yourself.

0
On

For UserVo you will have

UserVo implements Parcelable {
...
...
public UserVo (){

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

  @Override public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(name);
    + other data ...writInt etc 

}

public void readFromParcel(Parcel source){
    name = source.readString();
    + other data ..keep the same order
}
public static final Parcelable.Creator<DocumentFile> CREATOR = new Parcelable.Creator<UserVo>() {

    @Override
    public UserVo createFromParcel(Parcel source) {
        return new UserVo(source);
    }

    @Override
    public UserVo[] newArray(int size) {
        return new UserVo[size];
    }
};
public UserVo(Parcel source )
{
    readFromParcel(source);
}

Also have a look at annotations library , it will be easly to send data using @Extra annotation .

0
On

In order to send the custom objects from one Activity to Other, you have to serialize the objects with serialVersionID. Try the below modified code for your requirement.

UserVo.java

public class UserVo implements Serializable {

private static final long serialVersionUID = 1L;

String name,id;
String can_reply,can_view;

public String getName() {
    return name;
} 

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

public String getId() {
    return id;
} 

public String getCan_reply() {
    return can_reply;
} 

public void setCan_reply(String can_reply) {
    this.can_reply = can_reply;
} 

public String getCan_view() {
    return can_view;
} 

public void setCan_view(String can_view) {
    this.can_view = can_view;
} 

public void setId(String id) {
    this.id = id;
} 

ActivityA.java - To send the object through Intent

Intent objectIntent = new Intent(ActivityA.this, ActivityB.class);
i.putExtra("sendObjects", UserVo ); //Your object here
startActivity(objectIntent);

ActivityB.java - To Receive the Intent

UserVo mReceiveObjects = (UserVo)getIntent().getExtras().get("sendObjects");