tags:

views:

63

answers:

1

I have two activities, activity 1 and activity 2 for example. Activity 1 will call activity 2 and send an arraylist which will then be modified. This I have already done using an intent. What I now want to do, is when activity 2 calls finish() I want that modified arraylist to be sent back to activity 1 so that it has the most upto date version of that arraylist.

Activity 1:

Bundle b = new Bundle();
b.putParcelableArrayList("com.Woody.RingerSchedule", schedules);
Intent i = new Intent(this, addSchedule.class);
i.putExtras(b);
startActivity(i);

Activity 2 so far:

Bundle b = getIntent().getExtras();
final ArrayList<Schedule> schedules = b.getParcelableArrayList("com.Woody.RingerSchedule");
//modify arraylist
//need code here to return arraylist to activity 1
finish();

Any help appreciated.

+2  A: 

You need to call setResult with the Intent param

Intent intent = new Intent();
intent.putExtra("returnKey","test");
setResult(RESULT_OK,intent);
finish();

You read from the activity that started the activity with startActivityForResult

//we need a handler for when the secondary activity finishes it's work
//and returns control to this activity...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    super.onActivityResult(requestCode, resultCode, intent);
    Bundle extras = intent.getExtras();
    mEditText1.setText(extras != null ? extras.getString("returnKey"):"nothing returned");
}

See this for more example: http://www.remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html

Pentium10
Ok, i have added that to activity 2, how do I then pick that up in activity 1?
Woody
Check the updated answer.
Pentium10
Thankyou, worked perfectly.
Woody
Important on SO, you have to mark accepted answers by using the tick on the left of the posted answer, below the voting. This will increase your rate.
Pentium10