john123
john123

Reputation: 589

Why new Activity's onDestroy() is called?

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?

Upvotes: 0

Views: 1411

Answers (2)

Stefan de Bruijn
Stefan de Bruijn

Reputation: 6319

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

Upvotes: 1

Rahul
Rahul

Reputation: 45070

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.

Upvotes: 0

Related Questions