Android passing an arraylist back to parent activity
I've been searching for a simple example of this with no luck.
In my android application I have two activities: 1. The main activity which is launched at startup 2. A second activity which is launched by pressing a button on the main activty.
When the second activity is finished (by pressing a button) I want it to send back an ArrayList of type MyObject to the main activity and close itself, which the main activity can then do whatever with it. How would I go about achieving this? I have been trying a few things but it is crashing my application when I start the second activity.
When the user presses button to launch second activity:
Intent i = new Intent(MainActivity.this, secondactivity.class);
startActivityForResult(i, 1);
The array which is bundled back after pressing a button on the second activity:
Intent intent= getIntent();
Bundle b = new Bundle();
b.putParcelableArrayList("myarraylist", mylist);
intent.putExtras(b);
setResult(RESULT_OK, intent);
finish();
And finally a listener on the main activity (although I'm not sure of 100% when this code launches...)
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && requestCode==1){
Bundle extras = data.getExtras();
final ArrayList<MyObject> mylist = extras.getParcelableArrayList("myarraylist");
Toast.makeText(MainActivity.this, mylist.get(0).getName(), Toast.LENGTH_SHORT).show();
}
}
Any ideas where I am going wrong? The onActivityResult() seems to be crashing my application.
EDIT: Class with parcelable methods:
import android.os.Parcel;
import android.os.Parcelable;
public class Plan implements Parcelable{
private String name;
private String id;
public Plan(){
}
public Plan createFromParcel(Parcel in) {
Plan plan = new Plan();
plan.setId(in.readString());
plan.setName(in.readString());
return plan;
}
public Plan(String name, String id){
this.name = name;
this.id = id;
}
public String getName(){
return name;
}
pub开发者_StackOverflow中文版lic void setName(String name){
this.name = name;
}
public String getId(){
return id;
}
public void setId(String id){
this.id = id;
}
public String toString(){
return "Plan ID: " + id + " Plan Name: " + name;
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(name);
}
public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
public Plan createFromParcel(Parcel in) {
return new Plan();
}
@Override
public Plan[] newArray(int size) {
// TODO Auto-generated method stub
return new Plan[size];
}
};
}
After the second activity finished, onactivityresult is called, but nothing displays inside the toast, its blank. any ideas? I'm guessing my class is still messed up...
EDIT: got it to work
I had the method that Peter supplied in the wrong place. It should be inside creator, like this:
public static final Parcelable.Creator<Plan> CREATOR
= new Parcelable.Creator<Plan>() {
public Plan createFromParcel(Parcel in) {
Plan plan = new Plan();
plan.setId(in.readString());
plan.setName(in.readString());
return plan;
}
and not out on its own.
Many thanks to Peter! Hope this helps someone else.
Your class MyObject
must implement Parcelable
in order to be serialized/deserialized by Android when put inside Bundle.
Update:
The method name is createFromParcel
. And actually you have to create your object Plan from data in the parcel:
public Plan createFromParcel(Parcel in) {
Plan plan = new Plan();
plan.setId(in.readString());
plan.setName(in.readString());
return plan;
}
精彩评论