1. Definition and Importance

1.9. CORE APPLICATION COMPONENTS IN ANDROID DEVELOPMENT

Android applications are built using four core components that define how an app interacts with the system and other apps. These components are:

  1. Activities
  2. Fragments
  3. Services
  4. Broadcast Receivers

Each of these components has a specific role in ensuring the smooth operation of an Android app.

1. Activities

Definition

An Activity represents a single screen with a user interface (UI) in an Android application. It is the entry point for user interactions and is responsible for displaying content and handling user input. An Activity represents a single, focused screen with a user interface. Think of it as a window in your app. Each Activity is responsible for a specific task or feature. For example, a messaging app might have separate Activities for the contact list, the chat window, and the settings screen.


Key Characteristics

  • Activities follow a lifecycle, which determines how they behave when the user navigates through the app.
  • Each activity has its own layout file that defines its UI.
  • Activities communicate with each other using Intents.

Lifecycle of an Activity

An activity goes through different stages in its lifecycle, managed by lifecycle callbacks. Activities have a lifecycle, a series of states they go through from creation to destruction. Understanding this lifecycle is crucial for managing resources and ensuring your app behaves correctly. Key lifecycle methods include:


  1. onCreate(): Called when the activity is first created. This is where you typically initialize UI elements, set up listeners, and bind data.
  2. onStart(): Called when the activity becomes visible or to the user
  3. onResume(): Called when the user interacts with the activity. Called when the Activity is in the foreground and ready to interact with the user.
  4. onPause(): Called when the activity goes into the background. Called when the Activity is about to be paused (e.g., another Activity is starting). You should save any unsaved data here.
  5. onStop(): Called when the activity is no longer visible.
  6. onDestroy(): Called when the activity is destroyed. Called before the Activity is destroyed. You should release any resources here.