Kotlin and Android Activity Lifecycle

onCreate(savedInstanceState: Bundle?) Is the activity which is run when the activity is first created, this method is called. It is a great place for all those tasks you want to first happen when the Android App loads up. The onCreate method is passed arguments defined in the Bundle object that would contain dynamic state details from a previous loading of the activity.

onRestart() Is called when the applications activity is about to restart again after having previously been stopped by the Android run-time process.

onStart() Is called straight after the call to the onCreate() or onRestart() methods has been made, onStart() lets the activity know that the is it going to become available to the user for interaction.

onResume() Is called when the activity is at the top of the activity stack and is the activity with which the user is currently using.

onPause() Is called when an activity is about to become the foreground activity. This call will be followed by a call to either the onResume() or onStop() method depending on whether the activity moves back to the foreground or becomes invisible to the user. Steps may be taken within this method to store persistent state information not yet saved by the app. To avoid delays in switching between activities, time consuming operations such as storing data to a database or performing network operations should be avoided within this method. This method should also ensure that any CPU intensive tasks such as animation are stopped.

onStop() – The activity is now no longer visible to the user. The two possible scenarios that may follow this call are a call to onRestart() in the event that the activity moves to the foreground again, or onDestroy() if the activity is being terminated.

onDestroy() – The activity is about to be destroyed, either voluntarily because the activity has completed its tasks and has called the finish() method or because the runtime is terminating it either to release memory or due to a configuration change (such as the orientation of the device changing). It is important to note that a call will not always be made to onDestroy() when an activity is terminated.

onConfigurationChanged() – Called when a configuration change occurs for which the activity has indicated it is not to be restarted. The method is passed a Configuration object outlining the new device configuration and it is then the responsibility of the activity to react to the change.

In addition to the lifecycle methods outlined above, there are two methods intended specifically for saving and restoring the dynamic state of an activity:

onRestoreInstanceState(savedInstanceState: Bundle?) – This method is called immediately after a call to the onStart() method in the event that the activity is restarting from a previous invocation in which state was saved. As with onCreate(), this method is passed a Bundle object containing the previous state data. This method is typically used in situations where it makes more sense to restore a previous state after the initialization of the activity has been performed in onCreate() and onStart().

onSaveInstanceState(outState: Bundle?) – Called before an activity is destroyed so that the current dynamic state (usually relating to the user interface) can be saved. The method is passed the Bundle object into which the state should be saved and which is subsequently passed through to the onCreate() and onRestoreInstanceState() methods when the activity is restarted. Note that this method is only called in situations where the runtime ascertains that dynamic state needs to be saved.