CS 4530 Midterm Review: Android App Programming

CS 4530 Mobile App Programming Midterm Review

MVVM Architecture

  • Model: Represents the app’s data and business logic.
  • View: Displays data from the ViewModel and captures user interactions.
  • ViewModel: Acts as an intermediary between the View and Model. Prepares and exposes data from the Model and handles user actions.
    • Lifecycle Management: Survives configuration changes (e.g., screen rotations), preserving UI state.

RecyclerView

  • Purpose: Efficiently display a scrolling list of items.
  • Key Components:
    • LayoutManager: Controls the layout of items (e.g., LinearLayoutManager, GridLayoutManager).
    • ViewHolder: Holds individual item views for better performance.
    • Adapter: Manages data and creates ViewHolders.
    • Animator: Manages item animations.
  • Advantages: More flexible than ListView, supports large datasets efficiently.

Intents

  • Purpose: Facilitate communication between components.
  • Explicit Intents: Specify a particular component (e.g., navigating to a specific Activity).
  • Implicit Intents: Request an action (e.g., opening a URL) and let the system choose the component.
  • Key Use Cases: Navigation between Activities, passing data between components, starting background services.

Fragments

  • Purpose: A portion of a UI or behavior within an Activity.
  • Advantages:
    • Reusability: Fragments can be used across multiple Activities.
    • Flexibility: Modular design for various screen sizes and orientations.
  • Lifecycle: Like Activities, Fragments have their own lifecycle (onCreate, onStart, etc.) that must be managed effectively.

Single-App Activity

  • An app design where the app has only one Activity that hosts multiple Fragments.
  • Advantages:
    • Simplifies navigation by centralizing logic.
    • Reduces the overhead of managing multiple Activity lifecycles.
    • Easier state management and data sharing between Fragments.

Kotlin Coroutines

  • Coroutine Scope: Defines the lifecycle of coroutines (e.g., tied to the lifecycle of an Activity or ViewModel).
  • Suspend Function: A function that can be paused and resumed without blocking the main thread.
  • Structured Concurrency: Ensures coroutines are properly scoped and canceled when needed, preventing leaks or unintended behavior.

Room Persistence Library

  • Purpose: Abstraction layer over SQLite that simplifies database interactions.
  • Key Components:
    • Entities: Represent tables in the database.
    • DAOs (Data Access Objects): Define methods for accessing and modifying data (queries, inserts, etc.).
    • Database Class: Connects the DAOs and Entities, tying everything together.
  • Advantages: Strong type safety, simpler queries, lifecycle-aware data access.

Repository in an MVVM

  • Role: Acts as an abstraction layer between data sources (e.g., database, network) and the ViewModel.
  • Benefits:
    • Keeps data logic separate from UI logic.
    • Makes it easier to switch data sources without affecting other parts of the app.

Activity Lifecycle

Activity Lifecycle in Android with Demo App - GeeksforGeeks

onCreate(): Initialize components.

onStart(): App becomes visible.

onResume(): App comes into the foreground (interaction starts).

onPause(): App moves into the background (interaction pauses).

onStop(): App is no longer visible.

onDestroy(): App is being destroyed.

Benefits of MVVM

MVVM improves code organization, testability, and maintainability:

  • Improved Code Organization: MVVM divides the app into Model, View, and ViewModel, allowing developers to work independently.
  • Enhanced Testability: ViewModel can be tested independently from the View.
  • Increased Maintainability: Clear separation of concerns makes the codebase easier to understand and maintain.

Storage Options in Android

  • SharedPreferences: For small amounts of key-value data.
  • Internal/External Storage: For larger files, private or public.
  • Room Persistence Library: For structured data, providing an object-oriented approach to database interaction.

Asynchronous Operations in Android

  • Challenges: Traditional solutions (Threads, callbacks, AsyncTask) can lead to complex code.
  • Kotlin Coroutines:
    • Suspend Functions: Cleaner, more readable asynchronous code.
    • Structured Concurrency: Proper management of coroutines, avoiding memory leaks.
    • Coroutine Scope: Limits coroutine lifetime, tied to lifecycle events.

Callbacks

Identify the asynchronous operation.

Find where the callback is passed.

Check what happens when the callback is triggered.

Coroutines

Identify where coroutines are launched (launch, async).

Follow the flow of suspend functions.

Check how errors are handled (try/catch or SupervisorJob).

Adding Features

General Process:

  • Feature Requirement: Identify the new feature’s purpose.
  • Files to Modify:
    • View: Update layout and UI components.
    • ViewModel: Modify or add data-handling logic.
    • Model/Repository: Update data sources if necessary.
  • View Binding/Fragment Navigation: Update navigation for new screens.
  • Testing: Write tests to validate the feature.

Debugging and Improving Code

Identifying Bugs:

  1. Reproduce the Bug: Identify trigger conditions.
  2. Check Logs: Use Logcat or custom logging.
  3. Trace the Call Stack: Examine the stack trace for crashes.
  4. Investigate UI Thread: Ensure UI operations are on the main thread.

Improving Code to Fix Bugs:

  1. Follow MVVM: Maintain separation of concerns.
  2. Null Safety: Use Kotlin’s null-safety features.
  3. Lifecycle Awareness: Use ViewModel, LiveData, or StateFlow.
  4. Concurrency Management: Properly scope and cancel coroutines.

Coroutines vs. Callbacks

  • Coroutines: Safer, less error-prone, avoids “callback hell”.
  • Callbacks: More prone to errors, necessary for older APIs or some libraries.

Glossary of Key Terms

  • Asynchronous Operation: Task running independently of the main thread.
  • Callback: Function executed later, typically after an asynchronous operation.
  • ConstraintLayout: Layout manager using constraints for UI element positioning.
  • Coroutine: Lightweight, concurrently executed component, suspendable and resumable.
  • Jetpack: Suite of Android libraries and tools.
  • LayoutManager: Controls item positioning in RecyclerView.
  • LiveData: Lifecycle-aware observable data holder.
  • Navigation Graph: XML representing navigation paths.
  • Singleton Pattern: Ensures only one instance of a class exists.
  • View: Basic UI building block.
  • ViewHolder: Holds references to views in a list item.
  • View Binding: Type-safe access to views from layout files.