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

4

7

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

    }
}
Octangular answered 19/8, 2016 at 4:22 Comment(13)
try to use startActivityJaan
@ChiragArora where exactly?Octangular
at place of startActivityForResult(intent, MessageThread.MessageType.IMAGE); use startActivity(intent);Jaan
@ChiragArora that didn't help.Octangular
are you use finish in activity after using startActivityJaan
this - #16204701 link might help you.Doyenne
What does exactly return true; does after finish() ?Maemaeander
@unknown I was trying to see whether if I exit the method, the activity would close. I have removed it and the error is still there.Octangular
Anything in the logcat? Don't filter the logcat you might miss something important.Basketwork
manifest for these 3 activities?Mcniel
Is onActivityResult even called?Desalvo
Add this in the manifest: android:noHistory=trueFlay
Have u extend custom activity?Protestant
M
2

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.

Misdo answered 29/8, 2016 at 11:17 Comment(0)
J
1

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

Jumbo answered 29/8, 2016 at 13:14 Comment(0)
R
0

It's working fine I have checked the same thing with 3 Activities: 1. MainActivity 2. SecondActivity 3. ThirdActivity

In MainActivity I have started SecondActivity by clicking on button, code is as follow:

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextclick();
            }
        });
    }

    public void nextclick()
    {
        Intent intent=new Intent(MainActivity.this, SecondAcivity.class);
        startActivityForResult(intent,1);
    }



@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode)
        {
            case 1:


                break;
        }
    }

In SecondActivity I have started ThirdActivity by clicking on floatingActionButton, code is as follows:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                nextclick();
            }
        });
    }

    public void nextclick() {
        Intent intent = new Intent(SecondAcivity.this, ThirdActivity.class);
        startActivityForResult(intent, 2);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 2:
                  //Set data for MainActivity
                    Intent intent = new Intent();
                    intent.putExtra("album", "dfdfd");
                    setResult(RESULT_OK, intent);
                    SecondActivity.this.finish();
                    break;
            }
        }
    }

In ThirdActivity I have started ThirdActivity by clicking on floatingActionButton, code is as follows:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
         //Set data for SecondActivity
            Intent data = new Intent();

            data.putExtra("album","album");
            setResult(RESULT_OK, data);
            finish();
        }
    });
}

Hope it will help you to find the exact problem in your code.

Rental answered 29/8, 2016 at 12:37 Comment(0)
T
-1

I tried your same logic with three activities like this

Activity A

    public class A extends AppCompatActivity {

    private static final int BCODE = 100;
    private String Tag="A Activity";
    Button triggerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        triggerButton= (Button) findViewById(R.id.triggerButton);
        triggerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent;
                Bundle arguments = new Bundle();
                intent = new Intent(A.this, B.class);
                intent.putExtras(arguments);
                startActivityForResult(intent, BCODE);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==BCODE){
            if(resultCode==RESULT_OK){
                Log.e(Tag,"succes");
            }
        }
    }
}

From activity A,i started activity B by passing intent with startActivityForResult

From activity B i am again doing the same thing

public class B extends AppCompatActivity {

    private static final int CCODE =200 ;
    private String Tag="Activity B";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_b);
        Intent intent;
        Bundle arguments = new Bundle();
        intent = new Intent(this, C.class);
        intent.putExtras(arguments);
        startActivityForResult(intent, CCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==CCODE){
            if(resultCode==RESULT_OK){
                Log.e(Tag,"suceess");
                setResult(RESULT_OK,new Intent());
                finish();
            }
        }
    }
}

In Activity C i am just finishing the activity after setting a result

public class C extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_c);
        setResult(RESULT_OK,new Intent());
        finish();
    }
}

When activity C finishes os will resume the activity B and also call onActivityResult().In onActivityResult of Activity B i am setting result and finishing the acitivity.Then OS will resume activity B and call onActivity result of activity A.I tried this many times this scenerio is working fine for me.

Tripartite answered 23/8, 2016 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.