Understanding Activity and Fragment Lifecycle

Tutorial 3 of 5

Understanding Activity and Fragment Lifecycle

1. Introduction

Goal of the Tutorial

This tutorial aims to help you understand the Android Activity and Fragment lifecycles. You will learn about the different states an activity or fragment can be in throughout its lifecycle and how to handle transitions between these states.

What Will You Learn?

By the end of this tutorial, you should be able to:

  • Understand the lifecycle of an Android Activity and Fragment.
  • Know the different states an activity or fragment can be in.
  • Handle transitions between different states.
  • Write more efficient and bug-free applications by understanding lifecycle events.

Prerequisites

  • Basic knowledge of Android development
  • Familiarity with Java or Kotlin programming languages

2. Step-by-Step Guide

Activity Lifecycle

An activity in Android has a lifecycle guided by the system calling methods on the activity instance. The main lifecycle methods are:

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

The activity moves from one state to another depending on the method being called. For example, when an activity is created, onCreate() is called, when the activity becomes visible, onStart() is called, and so on.

Fragment Lifecycle

A fragment, like an activity, also has a lifecycle. The main lifecycle methods are:

  • onAttach()
  • onCreate()
  • onCreateView()
  • onActivityCreated()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroyView()
  • onDestroy()
  • onDetach()

A fragment's lifecycle is closely tied to the lifecycle of its host activity. So when the activity is paused, all fragments in the activity are also paused.

Best Practices

Understanding the lifecycle methods is essential as it allows you to create efficient and bug-free applications. For instance, heavy work such as network calls or database transactions should be done at the right lifecycle state to avoid draining battery or causing the application to crash.

3. Code Examples

Here's an example of an activity with lifecycle methods:

public class MainActivity extends AppCompatActivity {

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

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

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

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

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

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

In this example, each lifecycle method logs its state:

public class MainActivity extends AppCompatActivity {

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

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

    // Other lifecycle methods with logs...
}

4. Summary

In this tutorial, you have learned about the Android Activity and Fragment lifecycles, the different states an activity or fragment can be in, and how to handle transitions between different states.

5. Practice Exercises

  1. Create an application and log all the calls to the activity lifecycle methods. Observe the order of the calls.
  2. Add a fragment to the application in exercise 1. Log all the calls to the fragment lifecycle methods. Compare the order of activity and fragment lifecycle method calls.
  3. Create an application that starts another activity. Log all the lifecycle methods in both activities and observe the order of the calls when the second activity is started.

Remember, the more you practice, the better you'll become. Happy coding!