This is my first attempt to ask for a solution to a problem of mine so try to be kind guys! I have found numerous solutions to problems I deal with in this site by doing simple search but this time I had no luck I guess. I found nothing to fit my problem so I ended asking a new question.
I 'm trying to pass an object through an intent from one activity to another. I have one abstract class and two subclasses (the one of them also abstract) as follows. I need them to be abstract. I removed a lot of code (abstract methods etc.) just to find what the problem is and I ended up with these really simple classes and I still have the same problem.
public abstract class MyObject {
private boolean New;
private boolean Modified;
public MyObject() {
New = true;
}
public boolean isNew() {
return New;
}
protected void setNew(boolean value) {
New = value;
}
protected boolean isModified() {
return Modified;
}
protected void setModified(boolean value) {
Modified = value;
}
}
public abstract class MyItemBase extends MyObject {
private Long Id;
private String Description;
public MyItemBase() {
super();
}
public Long getId() {
return Id;
}
public void setId(Long id) {
Id = id;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
}
public class MyItem extends MyItemBase implements Serializable {
private static final long serialVersionUID = -2875901021692711701L;
public MyItem() {
super();
}
}
I have two Activities, ActivityA with a button to call ActivityB as follows.
public class ActivityA extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
ActivityB.class);
MyItem item = new MyItem();
item.setId((long) 1);
item.setDescription("test");
intent.putExtra("MyItem", (Serializable) item);
startActivityForResult(intent, 1);
}
});
}
}
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
MyItem item = (MyItem) getIntent().getSerializableExtra("MyItem");
((TextView) findViewById(R.id.text)).setText(item.getDescription());
}
}
So when I receive MyItem from the second activity, with no errors at all, both properties Id and Description are null. Actually it's a new object because the New property is true. Now if I try to get the object exactly after I put it in the intent in the first Activity like that
intent.putExtra("MyItem", (Serializable) item);
MyItem newItem = (MyItem) intent.getSerializableExtra("MyItem");
startActivityForResult(intent, 1);
it works fine. newItem has all the properties with the correct values.
Any ideas?
Thanx in advance everybody!
From the official Serializable doc:
Your base class doesn't implement
Serializable
, so its fields are not getting serialized.Make
MyItemBase
(or evenMyObject
)Serializable
and all of its descendants will also becomeSerializable
(without explicitly implementing the interface).