Don’t forget the lifecycle of the Android Activity!

I just wanted to jot down a basic memo about Android’s lifecycle. Because I easily forget and that it is one of the crucial concept that you should know as an Android developer.

Activity lifecycle

If you are an Android developer, you should definitely know that the Android Activity has a “life cycle”, the lifetime of Activity. It is the flow from birth (until the screen is called) until it dies (to be discarded). By understanding the life cycle you can avoid the danger of bugs and other mess that might come in your way!

Here is the image of the lifecycle from the official Android developer page. The state of these lifecycles is defined as exactly the same name in ActivityClass as callBack. Please check with @Override.

android lifecylce

■OnCreate

Implementation of this method is mandatory. Activity is the first state. This state is set only the first time when Activity is called. We will primarily initialize the essential components of Activity. Also, load the layout for installing UI here.

.java 

setContentView(R.layout.activity_main); //Everything starts from here

■onStart

Called when Activity is called on the screen (degree). We will do the work here until you allow the user to use the UI.

@Override
    protected void onStart() {
       // TODO Auto-generated method stub
       super.onStart(); 
    }

■onRestart

OnRestart is called when the Activity is called again when the Activity is in the onStop state. OnStart is called after this process is done.

 @Override
    protected void onRestart() {
       // TODO Auto-generated method stub
       super.onRestart(); 
    }

■onResume

Called when the Activity is in the foreground.

    @Override
    protected void onResume() {
       // TODO Auto-generated method stub
       super.onResume(); 
    }

■onPause

Called before the user leaves the Activity. We will do it here if you move it with backGround or you need to save the data before it is closed. Also, if the user calls Activity that entered this state again, it will transition to onResume. Also, since this does not mean that Activity is never destroyed when it is called, you need to call onDestory if you want to do it when it is discarded. However, there is absolutely no possibility of being invoked, so you need to design it well.

    @Override
    protected void onPause() {
       // TODO Auto-generated method stub
       super.onPause(); 
    }

■onStop
Called when the Activity becomes invisible to the user.

    @Override
    protected void onStop() {
       // TODO Auto-generated method stub
       super.onStop(); 
    }

■onDestory
Called when Activity is done.

    @Override
    protected void onDestroy() {
       // TODO Auto-generated method stub
       super.onDestroy(); 
    }

Yes, I do know that it was so simple and just a basic, so I decided to post more tips about advanced pieces of stuff related to Android development!

Leave a Reply

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