search

Android Activity Lifecycle - Full explained with example

Comments:
Likes:
Shares:
Best Division

Best Division  @bestdivision

Published On - Last Updated -

Android Activity Lifecycle :-

MainActivity.java

package com.example.bestdivision;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

public class MainActivity extends AppCompatActivity {

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

        Log.d("Life Cycle Event: ", "I am In onCreate()");
    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.d("Life Cycle Event: ", "I am In onStart()");
    }

    @Override
    protected void onResume() {
        super.onResume();

        Log.d("Life Cycle Event: ", "I am In onResume()");
    }

    @Override
    protected void onPause() {
        super.onPause();

        Log.d("Life Cycle Event: ", "I am In onPause()");
    }

    @Override
    protected void onStop() {
        super.onStop();

        Log.d("Life Cycle Event: ", "I am In onStop()");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        Log.d("Life Cycle Event: ", "I am In onDestroy()");
    }

    @Override
    protected void onRestart() {
        super.onRestart();

        Log.d("Life Cycle Event: ", "I am In onRestart()");
    }
}

Here is the full explanation of the Android Activity Lifecycle, along with an example:

  1. **onCreate**: This is the first lifecycle method that is called when an activity is created. This method is used to initialize the activity, set its layout, and perform other one-time setup tasks.

Example:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("Activity Lifecycle", "onCreate");
}
  1. **onStart**: This method is called after **onCreate**, when the activity is starting and about to become visible to the user.

Example:

@Override
protected void onStart() {
    super.onStart();
    Log.d("Activity Lifecycle", "onStart");
}
  1. **onResume**: This method is called after **onStart**, when the activity has become visible and is about to start interacting with the user. This is typically the best place to start animations, music, and other operations that can consume resources.

Example:

@Override
protected void onResume() {
    super.onResume();
    Log.d("Activity Lifecycle", "onResume");
}
  1. **onPause**: This method is called when an activity is about to go into the background, either because a new activity is starting or because the app is about to be closed. This is a good place to save data, stop animations and other operations that should not continue in the background.

Example:

@Override
protected void onPause() {
    super.onPause();
    Log.d("Activity Lifecycle", "onPause");
}
  1. **onStop**: This method is called when an activity is no longer visible to the user. This is a good place to release resources and stop any operations that are not needed in the background.

Example:

@Override
protected void onStop() {
    super.onStop();
    Log.d("Activity Lifecycle", "onStop");
}
  1. **onDestroy**: This method is called when an activity is about to be destroyed. This is a good place to release resources and stop any ongoing operations.

Example:

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

The activity lifecycle methods are called in a specific order, and the order may change based on the actions of the user or system. Understanding the lifecycle and using these methods correctly can help you create a responsive and stable Android app.

Similar Blogs
0 Commentssort Sort By

@abcd 1 days ago

Aquí los que apoyamos a Los del limit desde sus inicios..

dislike
reply
sdfsdf