Android Wdigets, Activities and Notifications - Overview

Snackbar

  • Provides lightweight feedback about an operation.
  • Displays a brief message at the bottom of the screen.
  • Automatically disappears after a timeout.
  • Similar to a Toast but can include an action button.
  • Implemented using the Snackbar class.

Activity

  • Main activity is configured in AndroidManifest.xml.
  • Each activity must be declared in the manifest file.
  • Activities are managed as an activity stack:
    • When an activity starts another, it is pushed on top of the stack.
    • Pressing the Back button pops the current activity from the stack.

Activity Lifecycle

  1. Non-existent → Stopped → Paused → Running
  2. Developers manage state transitions using lifecycle callbacks:
    • onCreate() → onStart() → onResume()
    • onPause() → onStop() → onDestroy()

Lifecycle Details

  • onPause()

    • Activity is partially visible but not in focus.
    • UI updates may still continue.
    • Multi-resume is supported on select devices.
  • onStop()

    • Activity is no longer visible.
  • onRestart()

    • Always followed by onStart().
    • Called when an activity comes back to the foreground after being stopped but not destroyed.
    • Example: User presses the Home button and later reopens the app.
    • Used to refresh resources or UI elements.
  • onResume()

    • Called every time the app comes to the foreground.
    • Example: User switches to another app and then returns.
    • Used to:
      • Resume animations.
      • Refresh dynamic UI.
      • Start listening to sensors.

Android Intent

  • Used to move from one activity to another or start another app.
  • Two types:
    • Explicit Intent – Targets a specific activity.
    • Implicit Intent – Requests an action from any app that can handle it.

Intent Components

  • Action

    • Determines the structure of the intent.
    • Can be a predefined constant (provided by Android) or a custom action string (declared in AndroidManifest.xml).
    • Example actions: Open Camera, Make a Call, Start Voice Command.
  • Data

    • Specifies the data to operate on, typically a URI (e.g., file, contact).
    • Works with implicit intents.
  • Category (Optional)

    • Adds context about how the intent should be handled (e.g., "Map""Calendar").
    • Helps Android determine which activities/components can respond.
    • Can modify intent behavior (e.g., open in a specific mode).
  • Extra

    • Key-value pairs used to pass additional data.
    • Works with both implicit and explicit intents.
    • Can store primitive types and is delivered to the target activity.

Alert Dialog

  • popup window that includes:
    • Title
    • Message
    • List of selectable items (can use custom views like checkboxes)
    • Action buttons (typically 3: Positive, Negative, Neutral)

Android Notifications

  • Managed by Android’s Notification Management Service.
  • Components of a notification:
    • Small Icon – Set by the developer.
    • App Name – Automatically assigned.
    • Large Icon – Set by the developer.
    • Timestamp – Automatic (can be overridden by the developer).
    • Title & Text – Set by the developer.

Notification Features by Android Version

  • Android 7.0 – Supports direct reply from notifications (RemoteInput).
  • Android 10 – Introduced AI-generated action buttons (e.g., Yes, No, Thanks, or opening maps for an address) with MessagingStyle notifications.

Notification Channel (Introduced in Android 8.0):

  • Every notification must be assigned to a channel.
  • Users can control settings for each channel, including:
    • Enable/Disable notifications.
    • Set importance level.
    • Customize sound and visualization.
  • Example Use Case: Instant messaging apps – each chat can have a separate notification channel.
  • Requires a channel ID.

Comments