This is the class that implements Parcelable:
public class MyUser implements Parcelable{
private String userName;
private String password;
MyUser(String u, String p){
userName = u;
password = p;
}
private MyUser(Parcel p){
Log.i("Parcel p", p.readString());
userName = p.readString();
password = p.readString();
}
// Getters
String getUserName(){ return userName; }
String getPassword(){ return password; }
// Setters
void setUserName(String u){ userName = u; }
void setPassword(String p){ password = p; }
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(userName);
dest.writeString(password);
}
public static final Parcelable.Creator<MyUser> CREATOR = new Parcelable.Creator<MyUser>() {
@Override
public MyUser createFromParcel(Parcel source) {
return new MyUser(source);
}
@Override
public MyUser[] newArray(int size) {
return new MyUser[0];
}
};
}
In FirstActivity:
username = (EditText) findViewById(R.id.username);
Intent secondStep = new Intent(this, SecondActivity.class);
mu = new MyUser(
username.getText().toString(),
password.getText().toString()
);
secondStep.putExtra("MyUser", mu);
Log.i(100 +": "+ mu.getUserName(), mu.getPassword());
startActivity(secondStep);
In SecondActivity:
Intent intent = getIntent();
Log.i("Second Activity", ": +intent.getExtras().getParcelable("MyUser"));
My Log is:
I/Second Activity: : com.example.username.sunshine.app.MyUser@1e8329b4
wheras I was aspecting to read the username written in the R.id.username
The object and the property are two different things:
MyUser mu = intent.getExtras().getParcelable("MyUser")
Log.i("Second Activity", ": +mu.getUserName());