Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Activity Lifecycle in Android with Demo App

In Android, the Activity lifecycle is a series of states through which an activity might go during its lifetime, from the moment it's created to when it's destroyed. Understanding these states is crucial for ensuring your app behaves as expected and doesn't consume resources unnecessarily.

The main methods in the Activity lifecycle are:

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()
  • onRestart()

Here's a simple demonstration of the Activity lifecycle:

  1. Create a new Android project with an Empty Activity.

  2. Replace the code in MainActivity.java with the following:

import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "LifecycleDemo";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate");
    }

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

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

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

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

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

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart");
    }
}

This code simply logs a message every time one of the lifecycle methods is called.

  1. Run your app.

  2. Monitor the Logcat output (filter by "LifecycleDemo" to only see the relevant logs). You'll see messages as your activity goes through various states:

  • When you first launch the app:
onCreate
onStart
onResume
  • If you press the home button:
onPause
onStop
  • When you return to the app from the recent apps screen:
onRestart
onStart
onResume
  • If you rotate the device (assuming you haven't fixed the orientation):
onPause
onStop
onDestroy
onCreate
onStart
onResume
  • When you close the app:
onPause
onStop
onDestroy

Understanding these callbacks is crucial for handling operations appropriately. For instance, network operations might be started in onStart() and stopped in onStop(), while UI updates would typically be handled in onResume() and paused or stopped in onPause().

  1. Activity Lifecycle methods in Android:

    These methods represent different states in the lifecycle:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Initialization code
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        // Code for when the activity starts
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // Code for when the activity resumes
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // Code for when the activity pauses
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        // Code for when the activity stops
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // Cleanup code
    }
    
  2. Android Activity Lifecycle diagram:

    The Activity Lifecycle can be visualized with a diagram showing the transitions between different states. It's a graphical representation of the sequence of method calls during the lifecycle.

  3. Activity Lifecycle example app in Android:

    Creating a simple app to log the lifecycle methods:

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Log.d("Lifecycle", "onCreate called");
        }
    
        // Implement other lifecycle methods similarly
    }
    
  4. Demo app illustrating Android Activity Lifecycle:

    Create a simple app with multiple activities, and log the lifecycle methods in each activity. Observe the logs as you navigate through the app to understand the lifecycle transitions.

  5. Handling configuration changes in Android Activity Lifecycle:

    When the device configuration changes (e.g., screen rotation), the activity is recreated. To handle this, you can use the onSaveInstanceState() and onRestoreInstanceState() methods to save and restore the activity state.

  6. Activity Lifecycle flowchart in Android:

    A flowchart can provide a visual representation of the decisions made during the activity lifecycle. It can be complex but gives a comprehensive view of how the lifecycle methods interact.

    Here's a simplified version:

    +-----------+
    | onCreate  |
    +-----+-----+
          |
          v
    +-----+-----+
    | onStart   |
    +-----+-----+
          |
          v
    +-----+-----+
    | onResume  |
    +-----+-----+
          |
          v
    +-----+-----+
    | onPause  |
    +-----+-----+
          |
          v
    +-----+-----+
    | onStop   |
    +-----+-----+
          |
          v
    +-----+-----+
    | onDestroy|
    +-----------+