Android, finish() closes application insead of activity
Asked Answered
M

3

6

I call an activity for result:

private static final int CODE_LOGIN = 0;
private static final int CODE_DETAILS = 1;

private void callDetailsActivity() {
    Intent switchToDetailsActivity = new Intent(getApplicationContext(), Details.class);
    switchToDetailsActivity.putExtra(TAG_ID, details.get(TAG_ID));
    startActivityForResult(switchToDetailsActivity, CODE_DETAILS);
}

Now in my Details.class i call to get back to the previous activity:

@Override
public void onBackPressed() {
    setResult(RESULT_OK);
    super.onBackPressed();
}

And then my onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if (requestCode == CODE_LOGIN) {
                // This is for my other Activity, where the "return" works
        }
    }
    updateOffers();
}

But insead of going back to my previous class the application is closed without any error logs. When I press the home button to go to my previous application, I can go to my application and then I am in my previous activity, but thats definately not the way it is supposed to work.

I also tried not to change onBackPressed(), or simply write finish() into onBackPressed(), but nothing worked.

I haven't set android:noHistory="true"

With my other Activity (which uses excatly the same code), it works perfectly (CODE_LOGIN).

Can somebody help me?

Mahoney answered 18/10, 2013 at 12:31 Comment(6)
Can you also share the code for your "onActivityResult" in the first activity?Foreside
try to add super.onBackPressed in bottom of onBackPressed() and remove parameters from new Intent().Bereniceberenson
Are you calling finish() after startActivityForResult?Johnettajohnette
No, just what it is up there, I updated the code ;)Mahoney
What is the value of the constant CODE_DETAILS?Salisbury
Might be late answer: possibly check whether activity contains launchmode as SingleinstanceAlexiaalexin
T
1

Change setResult(RESULT_OK, returnToOffers); to setResult(RESULT_OK); and get rid of the returnToOffers intent. I also recommend replacing finish() with super.onBackPressed() for future compatibility.

Like if in Android Lik-M-Aid (or whatever the next version is), they decide to do some special red glow effect when a user cancels an activity with the back button, you won't have to update your app to support it.

Thrown answered 18/10, 2013 at 13:11 Comment(2)
I have changed the code like you said, but it still doesn't work.Mahoney
Can you add your manifest to the question and point out which activity isn't working correctly? The problem might be in there.Thrown
B
4

Maybe you have declared the first activity as android:noHistory="true" in AndroidManifest?

Bodwell answered 18/10, 2013 at 13:9 Comment(0)
M
3

I found my mistake. Somewhere deep in my code I accidentally called finish(), so in global I called finish() twice, which leads to closing the application.

Thanks for you help and the advice to use super.onBackPressed()

Mahoney answered 18/10, 2013 at 16:14 Comment(0)
T
1

Change setResult(RESULT_OK, returnToOffers); to setResult(RESULT_OK); and get rid of the returnToOffers intent. I also recommend replacing finish() with super.onBackPressed() for future compatibility.

Like if in Android Lik-M-Aid (or whatever the next version is), they decide to do some special red glow effect when a user cancels an activity with the back button, you won't have to update your app to support it.

Thrown answered 18/10, 2013 at 13:11 Comment(2)
I have changed the code like you said, but it still doesn't work.Mahoney
Can you add your manifest to the question and point out which activity isn't working correctly? The problem might be in there.Thrown

© 2022 - 2024 — McMap. All rights reserved.