0

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

2 Answers2

1

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.

Community
  • 1
  • 1
Kia
  • 124
  • 1
  • 1
  • 10
1

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

Maxime Liege
  • 120
  • 1
  • 11