tags:

views:

1427

answers:

2

I have two activities in which I need to pass an object array between them. From the main activity, I start the secondary activity with startActivityForResult(), and pass the array as a ParcelableArray in a Bundle. This works fine to get the array to the secondary activity; I can pull the array out of the Bundle and manipulate it from there.

The problem I'm having is when I want to pass the array back to the calling activity. I need it to occur when a user presses the back/return button on the device. I've tried placing the array back into a Bundle, calling setResult() from within onPause() on the secondary activity, but I'm assuming this is the incorrect location to perform the task. I've reached that conclusion as getCallingActivity() within onPause() returns null.

What's the proper way to return a Bundle to the calling activity in this situation?

Main Activity:

    Bundle b = new Bundle();
    b.putParcelableArray("com.whatever.data", items);

    Intent folderView = new Intent(MainView.this, FolderView.class);
    folderView.putExtras(b);
    startActivityForResult(folderView, 1);

Secondary Activity:

protected void onPause () {
    super.onPause();

    Intent result = new Intent();

    Bundle b = new Bundle();
    b.putParcelableArray("com.whatever.data", items);

    result.putExtras(b);

    setResult(Activity.RESULT_OK, result);
}
A: 

Have you tried onDestroy instead of onPause? Alternatively, you could repeatedly call setResult within the second activity whenever the array is updated, as the result won't be passed back until the second activity is actually finished.

There's probably a better solution than either of those, but that's what came to mind.

Klondike
+1  A: 

IIRC, I've hooked onKeyDown() and watched for KeyEvent.BUTTON_BACK and called setResult() there. In Android 2.0, you'd hook onBackPressed() instead.

CommonsWare
Doing the processing in `onKeyDown()` did the trick. Thanks!
harrywynn