Android: Lifecycle

The following diagram is helpful in understanding the Android Activity Lifecycle. This diagram is from a google code lab which can be found here.

onCreate(): This is only called once, before the activity is visible. Handle any one-time initializations such as inflate the layout, define click listeners, initialize variables, or set up view binding. The Bundle parameter will be null the first time an activity is started, but if it’s been destroyed and recreated, the Bundle will contain any info that was saved in onSaveInstanceState().

onRestart(): This is not called when the activity first starts, but only after being restarted after having been previously stopped, such as when a user has gone back to the home screen, then reopens the Activity from the recents screen. This is where you can put code you want to call only if your app is not being started for the first time.

onStart(): This can be called many times and the activity is visible after onStart() runs. It gets called when the Activity returns to the foreground such as when screen is turned back on or when returning to the activity from the home screen or another activity.

onRestoreInstanceState(): You can restore data that was save in onSaveInstanceState(), though generally it is better to restore it in onCreate(). Restore it here in case it needs to be stored after onCreate(). The same Bundle is passed to both methods.

onResume(): This is always called after onStart() even if the activity is just starting and there is nothing to resume. This is where the app gains focus and the user can interact with it. When the activity has been paused, onResume() will be called when it regains focus.

onPause(): This is called when an Activity no longer has focus but is still visible on the screen. For example, when a user chooses to “share”, a dialog pops up which then has focus, but the Activity can still be seen behind the dialog. Code that runs in this method can block other things from displaying, so it should be kept light weight.

onStop(): This is called when an activity moves into the background, such as when a user returns to the home screen. After onStop() is called, the Activity is no longer visible on the screen, but it is still in memory in the background and resources are still in use.

onSaveInstanceState(): This is where you save any data you might need in case the activity is destroyed. It is called after onStop() even if onDestroy() is not called. Any data saved to the Bundle can be restored either in onCreate() or in onRestoreInstanceState().

onDestroy(): This is called once to clean up any resources used by the activity when it is shut down. Once an activity is destroyed, an Activity can be garbage collected. Activities can be destroyed by rotating the device, hitting the back button, by calling Activity.finish(), or when a user force-quits the app. Activities that have not been used for a long time may also be destroyed. First, it will be paused and stopped.

Fragment Lifecycle

Fragments have their own lifecycle depicted in the diagram below. You can learn more about the Fragment Lifecycle and see the original diagram in this codelab.

You can access the 5 Fragment states in the Lifecycle.State enum:

INITIALIZED: A new instance has been instantiated.
CREATED: The fragment lifecycle methods are called. The view associated with the fragment is created.
STARTED: The fragment is visible on screen but does not have focus.
RESUMED: The fragment is visible and has focus.
DESTROYED: The fragment has been de-instantiated.

Override the following methods in Fragment to respond to lifecycle events:

onCreate(): The fragment has entered the CREATED state. It has been instantiated but there is no view yet.

onCreateView(): This is where you inflate the layout (note this is different than Activity).

onViewCreated(): This is called after the view is created and is when you would bind views to properties (with findViewById()).

onStart(): The fragment has entered the STARTED state,

onResume(): The fragment has entered the RESUMED state. It has focus and can respond to user input.

onPause(): The fragment has re-entered the STARTED state and is visible.

onStop(): The fragment has re-entered the CREATED state. It is still instantiated but is not present on screen.

onDestroyView(): The view has been removed from memory but the fragment object still exists.

onDestroy(): The fragment has entered the DESTROYED state.