Java Android App Development Course, Activity Lifecycle

One of the most important concepts in Android app development is the Activity and its lifecycle. An Activity is a component that makes up the user interface (UI) and represents a screen that users interact with. In this course, we will explore the Activity lifecycle in Android app development using Java and explain the tasks that can be performed at each stage.

What is an Activity?

An Activity is a component that represents the user interface of an Android application and corresponds to a single screen within the app. Everything that the user interacts with in the app happens within an Activity. Activities include various UI elements to display information to the user and handle user interactions.

Activity Lifecycle

The Activity lifecycle refers to the flow of states and transitions from the creation to the destruction of an Activity. The Android system manages the Activity lifecycle, with the following key methods:

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

1. onCreate()

The onCreate() method is called when the Activity is first created. It primarily performs UI setup and basic initialization tasks. In this method, the layout is set and initial variables are defined.

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

2. onStart()

The onStart() method is called just before the Activity becomes visible to the user. In this method, the UI can be initialized, signaling that the Activity is ready for user interaction.

@Override
protected void onStart() {
    super.onStart();
    // Handle just before the Activity is shown to the user
}

3. onResume()

The onResume() method is called when the Activity is about to become the foreground activity. Common tasks performed here include updating the UI or starting animations.

@Override
protected void onResume() {
    super.onResume();
    // Update UI or start animations
}

4. onPause()

The onPause() method is called when another Activity is starting or this Activity is being paused. In this method, data should be saved, or animations should be stopped. It indicates that the user is no longer actively using this Activity.

@Override
protected void onPause() {
    super.onPause();
    // Save data and stop animations
}

5. onStop()

The onStop() method is called when the Activity is no longer visible to the user. At this stage, resources should be released, and tasks that need to be handled when the Activity is not visible should be performed.

@Override
protected void onStop() {
    super.onStop();
    // Release resources and perform cleanup
}

6. onDestroy()

The onDestroy() method is called when the Activity is finishing. In this method, memory can be released and final cleanup tasks can be carried out. However, this method may not be called, so it is advisable to save important data in onPause() or onStop().

@Override
protected void onDestroy() {
    super.onDestroy();
    // Final cleanup tasks
}

7. onRestart()

The onRestart() method is called when the Activity is being restarted after stopping. This method is called after onStop(). Here, tasks to restore the previous state are performed.

@Override
protected void onRestart() {
    super.onRestart();
    // Tasks when restarting the Activity
}

Lifecycle Example

Now, let’s create an example that incorporates the Activity lifecycle explained earlier. In this example, we will log the Activity’s state through button clicks.

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

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

        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "Button clicked!");
            }
        });
    }

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

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

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

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

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

Managing the Lifecycle

Managing the Activity lifecycle has a significant impact on the performance of the app and the user experience. Data that is necessary during the creation and destruction process should be saved in onPause() or onStop(), and the UI should be updated in onResume() when changes occur.

Saving State

When an Activity is restarted after being stopped, it may be necessary to save its state. You can use the onSaveInstanceState() method for this purpose. Below is an example of how to save state.

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("key", "value");
}

Saved state can be restored in onCreate() or onRestoreInstanceState().

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    if (savedInstanceState != null) {
        String value = savedInstanceState.getString("key");
    }
}

Conclusion

The Android Activity lifecycle is one of the fundamental components of an app, and understanding it and managing it effectively is important. Performing tasks correctly according to each stage of the lifecycle can provide a better user experience. I hope this course provides you with a basic understanding of the Activity lifecycle.

In conclusion, I have explained the Activity lifecycle in Android app development using Java. Deepen your understanding through practical exercises, and make good use of the lifecycle in various situations!