finish activity in onActivityResult does not work
Asked Answered
S

3

7

I have a pair of activities that must live or die together. Basically AlphaActivity does some work and then dispatches an intent (startActivityForResult()) for BetaActivity. When BetaActivity is done, I want it to dispatch an intent (startActivity()) for GammaActivity and then call finish() on itself. Upon finishing I was hoping for AlphaActivity's onActivityResult() method to be called, but that never seems to happen. My design is such that inside AlphaActivity's onActivityResult(), I call finish(). My plan is such that once GammaActivity is reached, a user cannot ever return to either AlphaActivity or BetaActivity. But presently the back button does take the user to the AlphaActivity.

I do have some ideas why it's not working, but discussing them here is pointless since I am interested in what might actually work.

EDIT:

The code is all quite standard stuff:

From inside Alpha

private void startBetaActivity() {
    Intent intent = new Intent(this, BetaActivity.class);
    startActivityForResult(intent, Constant.EXIT_CODE);

}

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

    if (resultCode == RESULT_OK) {
        if (requestCode == Constant.EXIT_CODE) {
            this.finish();
        }
    }
}

From inside Beta:

if (success) {
            startGammaActivity();
            finish();
        }
Stob answered 24/4, 2013 at 23:56 Comment(2)
What you describe should work. Can you post the code where you start BetaActivity and then finish it?Tableware
Do you need to do something with a result from BetaActivity? Why can't AlphaActivity it just finish() when it starts BetaActivity?Ledda
C
6

I think you just need to:

if (success) {
        startGammaActivity();
        setResult(Activity.RESULT_OK); //add this
        finish();
}
Contraception answered 25/4, 2013 at 0:24 Comment(0)
R
5

In my perspective you should follow this,

  1. AlphaActivity starts BetaActivity for result with X request code
  2. BetaActivity does his work and then calls setResult(Y, Z) and call finish()
  3. AlphaActivity will run onActivityResult with RequestCode X, ResultCode Y and data Z. If X and Y are the ones that you are expecting then starts GammaActivity and finally call finish() on AlphaActivity

You should not start GammaActivity on BetaActivity because the AlphaActivity onActivityResult will not work properly.

Rolland answered 25/4, 2013 at 0:18 Comment(0)
M
4

You have not called setResult()

if (success) {
        startGammaActivity();
        setResult(RESULT_OK);
        finish();
    }

Or if you never need to go back from BetaActivity to AlphaActivity then in both activities manifest put android:noHistory=true

Mazur answered 25/4, 2013 at 0:22 Comment(2)
Thanks for the input, especially the bit about android:noHistory=true. I accepted the other because it looks like his was first. +1Stob
You did the right thing, I voted him up too. By the way if you ever setResult in onBackpress then you have to setResult before calling super()Mazur

© 2022 - 2024 — McMap. All rights reserved.