0

My code destroy my current activity and start a new activity, like below:

Intent intent = myActivity.getIntent();

myActivity.finish(); //Destroy my activity

myActivity.startActivity(intent); //Start my new activity

It works, the previous activity is destroyed and new activity starts, but AFTER start the new activity, the activity's onDestroy() method is called, why?

ShreeshaDas
  • 2,042
  • 2
  • 17
  • 36
john123
  • 589
  • 3
  • 6
  • 16

2 Answers2

1

Start your new Activity first, and then finish the old.

Stefan de Bruijn
  • 6,289
  • 2
  • 23
  • 31
0

From the android docs:-

protected void onDestroy ()

Perform any final cleanup before an activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method.

Community
  • 1
  • 1
Rahul
  • 44,383
  • 11
  • 84
  • 103
  • Please check my question carefully. Of course onDestroy() will be called because I destroy the previous activity, my question is why the onDestroy() is called after start a new activity. – john123 Feb 28 '13 at 08:11
  • 1
    I guess he means onDestroy is called on the activity that has just been started... having same question my self – Rami Dabain Aug 25 '16 at 22:37