Passing data in startActivityForResult and getting it back in onActivityResult
I have a ListView with a c开发者_StackOverflow中文版ontext menu where the user can take a picture. I take the picture like this:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
this.startActivityForResult(intent, PICTURE_RESULT);
and retrieving it like this:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICTURE_RESULT) {
long itemId = ???
savePicture(itemId, data.getExtras());
}
}
How do I pass along the ListView item id? I tried intent.putExtras and intent.getLongExtra, but the data is not passed with the result intent. I can't use an instance variable because the activity can get destroyed if the user swaps to landscape when taking the picture.
Store that variable as an Activity class member and then you can use it from every place of that Activity class. If you want to save variable state then you should use onSaveInstance state callback. Refer to the developer docs.
精彩评论