post-ad

Activity Life Cycle In Android?

When a user navigates within an app, the Android lifecycle assists developers in determining which states activities go through. As a result, we are able to perform proper activities at the appropriate times while avoiding crashes and other issues.

There is a lifespan for Android apps. When a user opens and closes an application, it passes through numerous stages.

When a user starts, pauses, resumes, cancels, or destroys an activity, the state of the application helps you manage it.

These are controlled using callback methods. You can override these methods to execute a particular operation in order to provide users with the intended result.

Six major lifecycle stages or callbacks are performed by an Android activity. onCreate(), onStart(), onResume(), onPause(), onStop(), and onDestroy() are the functions available. Each of these callbacks is activated by the system.

It’s worth noting that you don’t have to implement all of these lifecycle callbacks in your Android app. Depending on the complexity of the activity, you should know when to implement each of these callbacks as a developer.

A simple graphic depicting how users interact with an activity lifecycle is below.

Activity life cycle

 

Let's go through the lifespan of this activity and how it is used in an Android app.

onCreate()

The onCreate() callback is compulsory in all Android applications. It is the first method called when we launch an activity from the home screen or intent. In other words, it is a default callback that is automatically created when you create a new activity.

It’s the only method that developers need to implement activity logic that should only happen once, such as initializing a ViewModel.

Android Studio automatically creates a class named the MainActivity.java file. This class contains an onCreate() callback. It is called when a user first opens the application.

private static final String TAG = "MainActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Toast.makeText(this, "onCreate MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onCreate MainActivity");
}

onStart()

When an application is started, the system will invoke an onStart() method. This callback is invoked to make the activity visible to the user.

Here is how onStart() is implemented.

@Override
protected void onStart() {

    Toast.makeText(this, "onStart MainActivity", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onStart MainActivity");

    super.onStart();
}

onResume()

Once onStart() is called, onResume() is immediately invoked. Every component associated with this activity is brought to the foreground state. The activity is now considered interactive.

@Override
protected void onResume() {

    Toast.makeText(this, "onResume MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onResume MainActivity");
    super.onResume();

}

onPause()

onPause() is called when the user switches to another activity or a multi-window mode application. At this point, the activity has lost focus and is running in the background.

This callback will pause the activity and release some resources that this activity was consuming. All un-required operations are paused.

@Override
protected void onPause() {

    Toast.makeText(this, "onPause MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onPause MainActivity");

    super.onPause();
}

onStop()

At this point, most of the activity processes have been stopped. However, the activity is still running in the background.

This life-cycle usually occurs after the onPause() method is executed due to the user switching to other activities or pressing the home button.

@Override
protected void onStop() {

    Toast.makeText(this, "onStop MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onStop MainActivity");

    super.onStop();
}

onRestart()

Since the activity’s states still exist, the onRestart() method can be called when the user restarts the activity. This means the activity will go back to the main screen and the user can resume interacting with its components.

As discussed, the onCreate() function is only called once in an activity’s life-cycle. So, when the onRestart() method is executed, the activity will resume by executing the onStart() then onResume().

@Override
protected void onRestart() {

    Toast.makeText(this, "onRestart MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onRestart MainActivity");

    super.onRestart();
}

onDestroy()

This is the final callback that the activity will receive when it is stopped.

The method is called when there is a change in the configuration states such as screen rotation or language settings. The Android system will destroy the activity, then recreate it with the set configurations.

@Override
protected void onDestroy() {

    Toast.makeText(this, "onDestroy MainActivity", Toast.LENGTH_SHORT).show();
    Log.d(TAG, "onDestroy MainActivity");

    super.onDestroy();
}

Conclusion

As a developer, you should wisely choose what to execute when a certain method or callback is executed. You might not end up using all these callbacks in one application.

This tutorial has helped you learn the different lifecycle stages in an Android application.

I hope you find this tutorial helpful.

Submit a Comment

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

Subscribe

Select Categories

page-ad