Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created April 2, 2015 19:37
Show Gist options
  • Save kristopherjohnson/11e10799e24fe04a183c to your computer and use it in GitHub Desktop.
Save kristopherjohnson/11e10799e24fe04a183c to your computer and use it in GitHub Desktop.
Notes about Android Activity lifecycle method ordering

Results of some experiments to determine which Activity lifecycle methods get called in certain situations.

Scenario: Launch app from home, then return home

Launch:

  • activity.onCreate()
  • activity.onStart()
  • activity.onResume()
  • activity.onWindowFocusChanged(true)

Note: Launching already-running app is similar, except a call to activity.onRestart() will occur instead of onCreate().

Press Home:

  • activity.onUserInteraction()
  • activity.onUserLeaveHint()
  • activity.onPause()
  • activity.onWindowFocusChanged(false)
  • activity.onSaveInstanceState()
  • activity.onStop(); isChangingConfigurations() returns false

Scenario: App pushes new activity onto stack

(activity1 is the activity that was on the top of the stack. activity2 is the new activity being pushed on top.)

  • activity1.onPause()
  • activity2.onCreate()
  • activity2.onStart()
  • activity2.onResume()
  • activity1.onWindowFocusChanged(false)
  • activity2.onWindowFocusChanged(true)
  • activity1.onSaveInstanceState()
  • activity1.onStop(); isChangingConfigurations() returns false

If activity1 calls finish(), then activity1.onDestroy() will occur after onStop()

Scenario: User presses Back button to dismiss activity and reveal previous activity

(activity2 is the top-of-stack activity being dismissed. activity1 is the activity that is beneath activity2.)

  • activity2.onUserInteraction()
  • activity2.onPause()
  • activity1.onRestart()
  • activity1.onStart()
  • activity1.onResume()
  • activity1.onWindowFocusChanged(true)
  • activity2.onWindowFocusChanged(false)
  • activity2.onStop()
  • activity2.onDestroy()

Scenario: Orientation change in activity that does not implement onConfigurationChanged()

  • activity.onConfigurationChanged()

Scenario: Orientation change in activity that does implement onConfigurationChanged()

  • activity.onPause()
  • activity.onSaveInstanceState()
  • activity.onStop(); isChangingConfigurations() returns true
  • activity.onDestroy()
  • activity.onCreate()
  • activity.onStart()
  • activity.onRestoreInstanceState()
  • activity.onResume()
  • activity.onWindowFocusChanged(true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment