Categories
Android

Activities and their lifecycle

Understanding the activity lifecycle

An Activity is a very important android app component, representing an entry point for the user to interact with the application.

Activities can also be seen as containers that can be filled with different type of “Views” like buttons, text, images etc. Views are basic user interface components. The View class is the parent class of all the above mentioned android widgets.

If you ever created an android app you might have seen that there was no main() method. So, unlike regular Java for example, the Android system runs the code in an Activity by invoking different callback methods depending on the state of that activity.

The core set of activity callbacks are:

onCreate()

Gets called when your activity is first created. Given that this gets called only once during the activity lifecycle, you should do most of your initialization like creating views, inflating your layout or other stuff that you only want to get done once, here.

onStart()

Gets called when the activity is becoming visible to the user. Keep in mind that the activity will not stay very long in this particular state.

onResume()

Gets called when the activity is visible to the user and once the onStart() callback finishes. This is the state in which the user can interact with the app.

onPause()

Gets called when the activity is no longer in the foreground (it can still be visible in some situations) and it can be an indicator that the user is leaving the activity.

onStop()

Gets called when the activity is no longer visible to the user. For instance if you press the Home button on your android device the system will trigger this callback.

onDestroy()

Gets called when the activity is about to be destroyed. When exactly? Well for example if the user presses on the recent apps button and dismisses your app, every activity in your app will be destroyed. Another important situation to remember is that the system will invoke this callback when configuration changes occur (changing from portrait to landscape or the other way around).

Quick warning to avoid some possible confusion, because I remember I was a bit confused when I read about this topic. I thought that I had to implement all these callbacks into my app for the activity to go through all its states. The system will invoke these callbacks regardless of your implementation, so only implement what’s needed in your app.

In order to get some hands-on experience I would recommend to create a test app and implement the following callbacks into the app and create some logs. This way you can play around with the app and see which callback gets invoked in certain scenarios.

public class TestActivity extends AppCompatActivity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_test);  
        Log.d("activityLifecycle","onCreate callback");  
    }  

    @Override  
    protected void onStart() {  
        super.onStart();  
        Log.d("activityLifecycle","onStart callback");  
    }  

    @Override  
    protected void onResume() {  
        super.onResume();  
        Log.d("activityLifecycle","onResume callback");  
    }  

    @Override  
    protected void onPause() {  
        super.onPause();  
        Log.d("activityLifecycle","onPause callback");  
    }  

    @Override  
    protected void onStop() {  
        super.onStop();  
        Log.d("activityLifecycle","onStop callback");  
    }  

    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        Log.d("activityLifecycle","onDestroy callback");  
    }  
}  

Activity lifecycle is an important part of android development so make sure you understand it properly.

Comments are closed.