011 Room
Fragment Bindings and Room Database Setup
Introduction to Fragment Bindings
- The lecture will cover fragment bindings, view bindings for other fragments, and the initiation of a Room database.
- The goal is to enable navigation from the art fragment to the API fragment by clicking an image view.
Implementing Navigation
- The instructor discusses creating a binding variable for the art details fragment, which allows interaction with UI elements.
- A click listener is set on the art image view to navigate to the image API fragment using
findNavController.
Back Press Handling
- The importance of overriding back press functionality is highlighted; it allows custom actions when users press back.
- An
OnBackPressedCallbackobject is created to manage what happens during back presses, such as popping the back stack.
Room Database Overview
- Introduction to Room as a modern alternative for SQLite operations in Android development, moving away from manual content providers.
Introduction to Room Database in Android
Overview of Room Database
- The speaker introduces the concept of Room, a database library for Android, and suggests consulting the official documentation for beginners.
- Room is part of Android Jetpack and consists of three main components: the Room database, Data Access Objects (DAOs), and entities.
Components of Room
- DAOs serve as an interface between the Room database and entities, facilitating communication for data operations.
- Entities are defined using data classes that represent tables in the database. Key attributes include name, artist name, year (integer), image URL (string), and an auto-generated primary key.
Creating Entities
- The speaker explains how to define entity properties such as
name,artistName,year, andimageUrlwithin a data class.
- The primary key is annotated with
@PrimaryKey(autoGenerate = true)to allow automatic ID generation when creating new entries.
Defining DAO Interface
- A new interface called ArtDao is created to define functions for interacting with the database.
- Functions within this interface will be annotated with specific commands like @Insert, @Delete, or @Query to perform various operations on the database.
Implementing DAO Functions
- The speaker emphasizes using suspend functions for asynchronous operations due to potential time-intensive tasks involved in database interactions.
- An example function named insertArt is introduced which utilizes @Insert annotation; conflict strategies can also be specified during insertion.
Additional DAO Operations
- A deleteArt function is defined using @Delete annotation to remove art objects from the database.
- To retrieve data from the database, an observerArts function returns LiveData containing a list of art items. This function uses SQL queries annotated with @Query.
Final Steps: Creating Database Class
Creating a Room Database in Android
Overview of Database Structure
- The database should be defined as an abstract class to enhance its functionality and maintainability. The speaker refers to documentation for clarity on the structure.
- The database will utilize Room, a persistence library in Android, requiring annotation with
@Database. Entities must be specified, with "art" being the primary entity represented as a list.
- A version number is assigned to the database; starting at one, it can be incremented (e.g., version two) when changes are made to the schema or entities.
DAO Implementation
- An abstract function will be created within the database class that returns the Data Access Object (DAO), which facilitates interaction with the database.
- The speaker mentions using Hilt for dependency injection later in the process, indicating that this step is not yet implemented but is planned for future development.
Summary of Current Progress