📱

Understanding Android Activities and Lifecycle

Mar 22, 2025

Android Basics - Introduction to Activities and Lifecycle

Overview

  • New series focused on core Android concepts for native development.
  • Aimed at both beginners and those looking to deepen their Android knowledge.
  • Assumes familiarity with Kotlin.
  • UI building not covered here – check Jetpack Compose playlist for that.

Topics Not Covered

  • Topics that are outdated due to Jetpack Compose (e.g., Fragments).

Activities

  • Definition: A container for one or multiple screens in an app.
  • Role: Unit of interaction with the user; not just a screen container.
  • Entry Point: Activities serve as entry points (e.g., when a user clicks a link in another app).

Evolution of Activities

  • Past: One activity per screen.
  • Transition: Use of Fragments to bundle related screens.
  • Present (Jetpack Compose): Typically one activity for the whole app.

Activity Lifecycle

  • Lifecycle States:

    • Created: onCreate function initializes variables and UI content.
    • Started: onStart makes the activity visible.
    • Resumed: onResume allows user interaction.
    • Paused: onPause is called when another UI component comes to the foreground.
    • Stopped: onStop when the activity is no longer visible.
    • Destroyed: onDestroy frees all resources.
  • Lifecycle Diagram: Complex at first glance, but logical once understood.

Detailed Lifecycle Explanation

  • onCreate: Initialization phase; set up views and variables.
  • onStart: Activity becomes visible like a theater curtain opening.
  • onResume: Activity is in the foreground and interactive.
  • onPause: Activity still in memory, but not in focus (e.g., dialog displayed).
  • onStop: Activity not visible; system may reclaim resources.
  • onRestart: Called when returning to a stopped activity.
  • onDestroy: Final cleaning when activity finishes or system needs resources.

Special Cases

  • Configuration Changes: Trigger lifecycle restart (e.g., screen rotation, language switch).
  • System Behavior: onStop and onDestroy may not always be called.

Practical Code Explanation

  • Example showing lifecycle functions overridden in code.
  • Using logs to track lifecycle state transitions.

Key Takeaways

  • Use onPause for critical data saving as onStop/onDestroy might not be reliably called.
  • Lifecycle understanding is crucial for effective Android app management.

Conclusion

  • Regular updates with new videos every Wednesday and Sunday.
  • Encouragement to subscribe for continued learning.