I have three activities
MessagesAttachPhotoActivity
MessageGalleryFolderSelectorActivity
ImagePickerActivity
MessagesAttachPhotoActivity
calls MessageGalleryFolderSelectorActivity
with startActivityForResult()
.
MessageGalleryFolderSelectorActivity
activity displays the photo folders on the phone and one selects a folder.
ImagePickerActivity
is then called with setActivityForResult()
. Once an image is selected from ImagePickerActivity
, it is passed back to MessagesAttachPhotoActivity
via MessageGalleryFolderSelectorActivity
.
When I run the app for the first time, everything works fine. However, if I try to select an image again afterwards, MessageGalleryFolderSelectorActivity
does not close after setResult()
.
I have tried calling finish()
, this.finish()
, ((Activity)getApplicationContext()).finish()
, and super.onBackPressed()
without success.
Why does the activity not close on successive runs?
Intent intent;
Bundle arguments = new Bundle();
Bundle bundle;
intent = new Intent(this, MessageGalleryFolderSelectorActivity.class);
bundle = new Bundle();
bundle.putInt(Constants.INTENT_EXTRA_LIMIT, Constants.IMAGES_SELECT_LIMIT);
bundle.putInt("Request", MessageThread.MessageType.IMAGE);
intent.putExtras(bundle);
startActivityForResult(intent, MessageThread.MessageType.IMAGE);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_MODE,
ImagePickerActivity.MODE_MULTIPLE);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_LIMIT, 10);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SHOW_CAMERA, false);
imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album);
//imagesIntent.putExtra(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, images);
startActivityForResult(imagesIntent, MessageThread.MessageType.IMAGE);
Intent data = new Intent();
data.putParcelableArrayListExtra
(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages);
data.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM,album);
setResult(RESULT_OK, data);
finish();
return true;
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
ArrayList<Image> selectedImages = data.getParcelableArrayListExtra
(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES);
String album = data.getStringExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM);
Intent intent = new Intent();
intent.putExtra(ImagePickerActivity.INTENT_EXTRA_ALBUM, album);
intent.putParcelableArrayListExtra
(ImagePickerActivity.INTENT_EXTRA_SELECTED_IMAGES, selectedImages);
setResult(Activity.RESULT_OK, intent);
this.finish();
return;
} else if (resultCode == RESULT_CANCELED) {
}
}
You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.
Explanation. finish() − It is used to close the activity.
I think we can refer parent activity using getParent(). So, in the class MessageGalleryFolderSelectorActivity we can write ((Activity)getParent()).OnActivityResult(requestCode,resultCode,data) on the overrided onActivityResult. So, we are handover got values to parent without processing it.
Try to use finishAffinity()
instead of finish()
on the activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With