Alchete
Alchete

Reputation: 1649

finish() on an Activity closes app then briefly shows previous Application

In my App, Activity1 launches Activity2 with "startActivityForResult()".

When Activity2 is done, the following code is called:

Activity2.java

private void finishActivity(final boolean accepted)
{
  Intent returnIntent = new Intent();

  setResult(accepted ? RESULT_OK : RESULT_CANCELED, returnIntent);

  finish();
}

Activity1 receives this callback, and if the result is not RESULT_OK, it also finishes.

Activity1.java

@Override

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

  if (requestCode == Globals.REQUEST_CODE_TOS)
  {
    if (resultCode != RESULT_OK)
      finish();
  }
}

After Activity1 finishes the app closes, as expected.

THE PROBLEM --

However, after my app closes, instead of going to the home screen, a previous application on the stack briefly launches forward, then closes also! Then I get to the home screen.

In the Android bug tracker, I see a similar bug listed for the case when the back button is pressed, which I believe is the same as calling finish():

https://code.google.com/p/android/issues/detail?id=71067

I'm seeing this on a Nexus 5, running 4.4.4.

Is this just a bug? Or are there any workarounds for the behavior?

EDIT --

To clarify the behavior I desire:

In Google Maps, if you decline the TOS popup, the app closes and you go directly to the home screen. There is no awkward flash to some previously opened app.

Upvotes: 1

Views: 845

Answers (2)

Alex
Alex

Reputation: 990

This is decidedly the same issue as the one you linked in your question (I've been following it for months, as I own a Nexus 5 myself). It was actually fixed in the AOSP, but that particular fix doesn't seem to have found its way into the N5 yet. I've got my fingers crossed for a fix in Lollipop, but we'll see when that rolls out.

As for a workaround, I don't think any exists at the moment, although if I'm wrong on that I would love to know - I work on an app myself which does something similar (manually calls finish() the bottom-most activity in the stack when closed via the back button).

Upvotes: 2

Ridcully
Ridcully

Reputation: 23655

Might be the empty Intent you return via setResult(). Have you tried null here, instead?

Upvotes: 0

Related Questions