Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

finish() not closing activity when called after the first run

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?

Here is my code:

Calling MessageGalleryFolderSelectorActivity:

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);

ImagePickerActivity:

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);

Passing data back to MessageGalleryFolderSelectorActivity:

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;

Trying to pass data back to the initial calling activity but this activity does not close MessageGalleryFolderSelectorActivity:

@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) {

    }
}
like image 739
mungaih pk Avatar asked Aug 19 '16 04:08

mungaih pk


People also ask

What happens when you call finish () inside onCreate ()?

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.

How do you finish an activity immediately?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you check if activity is finished or not?

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.

Which method is used to close an activity o destroy () o finish () o STOP () o close ()?

Explanation. finish() − It is used to close the activity.


2 Answers

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.

like image 125
Mahesh Chakkarwar Avatar answered Nov 14 '22 23:11

Mahesh Chakkarwar


Try to use finishAffinity() instead of finish() on the activity.

like image 32
Hictus Avatar answered Nov 15 '22 00:11

Hictus