Activity Life Cycle

Activity Life Cycle

Activity life cycle call back methods

As Android developers, we view Activity as a fundamental component for launching a basic application. In the MainActivity file, we encounter the onCreate method. Let’s delve into its significance and understand why it’s essential.

Let me introduce the Activity before delving into its life cycle concept.

What is an Activity?

Simply put, it’s a frame or window that displays the GUI for user viewing.

Similar to other programming paradigms that begin with the main() method, Android Apps commence with an Activity instance. An app may include one or more Activities, but it must have at least one from which the app initiates.

The activity undergoes a life cycle with states such as Initialized, Created, Started, Resume, and Destroyed, as well as events like Create, Start, Resume Pause, Stop, and Destroy, each corresponding to seven callback methods*, ensuring smooth and successful device operations.*

Call Back Methods

  1. onCreate()

This function is invoked as the system creates an activity, transitioning it to the Created state. It executes fundamental application startup processes that occur just once throughout the entire activity lifespan.

It takes a savedInstanceState parameter, which is a Bundle object that may hold data from the previous activity’s saved state.

When a life-aware component is connected to the activity life-cycle, it receives the ON_CREATE event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the created state.

onCreate callback method

2. onStart()

This function is invoked as the app prepares the activity for user visibility and interaction, transitioning it to the Started state.

It doesn’t accept any parameter to the functional call.

When a life-aware component is connected to the activity life-cycle, it receives the ON_START event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the started state.

The app swiftly transitions through this state, moving on to the resume state and triggering the onResume() method.

onStart callback method

3. onResume()

This function is called when activity comes to foreground.

This represents the phase where the app engages with the user. The app remains in this state until an event occurs, diverting attention, like an incoming call, user navigation to another activity, or the screen turning off.

When the activity transitions from Paused to Resumed, onResume() is invoked. Utilize onResume() to reinitialize components released in onPause() and perform any necessary setup when the activity enters the Resumed state.

It doesn’t accept any parameter to the functional call.

When a life-aware component is connected to the activity life-cycle, it receives the ON_RESUME event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the resumed state.

onResume callback method

4. onPause()

This function is called by the system as the initial signal that the user is exiting your activity, although it doesn’t necessarily imply the activity is being destroyed. It signifies that the activity is no longer in the foreground but remains visible in multi-window mode

The entry into this state by the activity can occur due to the following reasons:

i. Halts the application execution.

ii. When a new semi-transparent activity, like a dialog, is launched, it temporarily pauses the underlying activity. The paused state persists as long as the activity is partially visible but lacks focus.

When an interruptive event happens, the activity transitions to the Paused state, triggering the onPause() callback in the system.

It doesn’t accept any parameter to the functional call.

When a life-aware component is connected to the activity life-cycle, it receives the ON_PAUSE event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the started state.

onPause callback method

5. onStop()

This function is invoked when the activity is no longer visible to the user.

It doesn’t accept any parameter to the functional call.

When a life-aware component is connected to the activity life-cycle, it receives the ON_STOP event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the started state.

6. onDestroy()

This function is called before the destroying the activity.

The system may invoke this call for the following reasons

i. The user dismisses the activity or calls finish()

ii. The activity is temporarily destroyed during configuration changes, like rotation and multi-window scenarios

It doesn’t accept any parameter to the functional call.

When a life-aware component is connected to the activity life-cycle, it receives the ON_DESTROY event. Annotated with @OnLifecycleEvent, a method is invoked to execute its body for the started state.

onDestroy callback method

7. onRestart()

This function is called the activity either comes back to interact with the user.

It doesn’t accept any parameter to the functional call.

onRestart callback methos

Thank you for reading! Feel free to share your suggestions and comments to improve technical blogging. Your input is valued and appreciated!

Did you find this article valuable?

Support Sandeep Kulkarni by becoming a sponsor. Any amount is appreciated!