Question

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?

Was it helpful?

Solution 3

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top