Grigory
Grigory

Reputation: 1922

Unusual android activity lifecycle

I wrote a game, and in logs from market i sometimes see following pattern:

06:02:13:835|INFO|1|MainActivity|MainActivity#2.OnCreate
06:02:13:932|INFO|1|MainActivity|MainActivity#2.OnStart
06:02:14:010|INFO|1|MainActivity|MainActivity#2.OnResume
...
06:09:27:688|INFO|1|MainActivity|MainActivity#2.OnPause
06:09:28:895|INFO|1|MainActivity|MainActivity#3.OnCreate
06:09:29:159|INFO|1|MainActivity|MainActivity#3.OnStart
06:09:29:319|INFO|1|MainActivity|MainActivity#3.OnResume
06:09:29:551|INFO|1|MainActivity|MainActivity#2.OnStop
06:09:29:596|INFO|1|MainActivity|MainActivity#2.OnDestroy

MainActivity instance #3 is created and started before MainActivity instance #2 is destroyed. What does this pattern mean ? Why does new instance start before previous has been destroyed ?

Obviously i don't create activity by hand. Users simply start game by tapping icon(at least i hope so).

Activity has singleTask launch mode.

Thank you!

Upvotes: 0

Views: 98

Answers (2)

Jorge Casariego
Jorge Casariego

Reputation: 22212

Suppose your application has two activities. Then this would be the life cycle

enter image description here

onDestroy will be called if you explicitly call finish(); yourself or like the example if you press Back button because pressing back key actually provokes finish() method on your activity, and it causes your activity to be paused->stopped->destroyed

Upvotes: 1

Luca Fagioli
Luca Fagioli

Reputation: 13349

The onDestroy callback is not guaranteed to be called. From the docs:

onDestroy() = The final call you receive before your 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: 1

Related Questions