Yury
Yury

Reputation: 20936

When finish() should be called?

I have an activity that calls second activity through an intent. After the second Activity is called I want to finish the first activity. Thus, I have the following code in onStop() method:

@Override
public void onStop() {
    super.onStop();
    if (shouldBeClosed) {
        finish();
    }
}

The problem is that my first activity is not closed. And I do not understand why?
Maybe I should put this code into onPause()?
But as I understand in this case, when activity loose focus (like after the call of a dialog) my activity can be closed.
So the question why this happens and how I can correct this behavior?

P.S. The variable shouldBeClosed is true. This is not the point.

EDIT

Here is the call of the second activity:

Intent intent = new Intent(this, AcSpContextAssign.class);
Bundle extras = new Bundle();
extras.putInt(Constants.KEY_FROM_ACTIVITY, Constants.FROM_AcSpNameCreate);
extras.putLong(Constants.KEY_SPID, spId);
intent.putExtras(extras);
startActivity(intent);

Upvotes: 0

Views: 663

Answers (1)

Pavel Dudka
Pavel Dudka

Reputation: 20934

Normally, you should call finish() for your first activity right after you send an intent. Something like:

...
startActivity(secondActivityIntent);
finish();

This will trigger onPause()->onStop()->onDestroy() chain for your first activity, so you can perform normal clean-up there

Upvotes: 2

Related Questions