Dwijraj Bhattacharyya
Dwijraj Bhattacharyya

Reputation: 349

On calling finish() before startActivity method, Activity transition takes place

In my android application i made the mistake of calling startActivity method after finish() but still I was able to move to the next activity. I want to know how this happened shouldn't the activity be destroyed before the startActivity is executed Here is a Sample Code

    Intent N=new Intent(A.this,B.class);
    finish();
    startActivity(N);

I am able to move to Activity B without any problem and also Activity A is destroyed

Upvotes: 0

Views: 1032

Answers (2)

Maxime Liege
Maxime Liege

Reputation: 120

it depends from where you called finish()

finish() in onCreate() will call onDestroy()

finish() in onStart() will call onCreate(), onStart(), onStop(), onDestroy()

finish() in onResume() will call onCreate(), onStart(), onResume(), onPause(), onStop(), onDestroy()

you can take a look on the Android life cycle :

https://developer.android.com/guide/components/activities/activity-lifecycle.html

Upvotes: 1

Kia
Kia

Reputation: 124

I don't believe finish() has the same effects as a return (otherwise we would get the unreachable statement error), so the rest of the flow still gets called.

More info.

Upvotes: 1

Related Questions