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.

Leave a Reply

Your email address will not be published. Required fields are marked *