ensuring that your apps are robust, testable, and maintainable. ❖ Android provides a set of libraries and components, such as ViewModel, Livedata, Room, to help you put together your app according to best practices. ❖ Architecture principles are ➢ Separation of concerns ➢ Drive your UI from a model
fragments, should only contain logic that handle UI and operating system interactions such as displaying data to user, permission request etc. ❖ By keeping UI classes lean, you can avoid many life-cycle related problems . ❖ Example: Configuration changes
your UI from a persistent model. ❖ This way ➢ Your app continues to work in cases of no network or users network is flaky. ➢ Models are unaffected by apps lifecycle. This way the user does not lose data in case OS destroys your app to clear up resources.
survive configuration changes such as screen rotation. ❖ ViewModel is scoped within the Lifecycle of the related UI component such as an Activity or a Fragment. ❖ The ViewModel remains in memory until the Lifecycle it is scoped goes away; the activity is destroyed or fragment detached. .
Live data updates can be observed by other components such as activities or fragments. ❖ It is lifecycle aware. ➢ It respects the lifecycle of other components such as activities or fragments. ➢ It notifies app components observers that are in an active lifecycle state about updates.
to be in active state if its lifecycle is in STARTED or RESUMED state. ❖ You can register an observer paired with an object that implements LifecycleOwner interface, i.e. Activity or a Fragment. ➢ This relationships allows observers to be removed when the state of the corresponding object changes to DESTROYED.
over SQLite to allow for more fluent database access. ❖ Room has three major components. ➢ The Database class ➢ Data entities ➢ Data Access Objects(DAOs)
Each entity is a data class that is annotated with @Entity. ❖ Each property in the class represents a column in the table. ❖ The following is an example of Student entity. @Entity(tableName = "student") data class Student( @PrimaryKey val id: Int, val name: String, val course: String )
methods that offer access to data stored in the app database. ❖ You can define a DAO either as an interface or an abstract class. For basic use cases you should use an interface.
➢ Convenience methods that let you insert, update, and delete rows in your database without writing any SQL code. ➢ Query methods that let you write your own SQL query to interact with the database.
that perform simple inserts, updates, and deletes without requiring you to write a SQL statement. ❖ @Insert: annotation allows you to define methods that insert their parameters into the appropriate table in the database. @Insert fun saveStudents(students: List<Student>)
methods that delete specific rows from a database table. @Delete fun deleteStudent(student: Student) ❖ @Insert, @Update and @delete methods accept data entity instances as parameters. ❖ If you need to define more complex inserts, updates, or deletes, or if you need to query the data in the database, use a query method instead.
statements and expose them as DAO methods. @Query("SELECT * FROM student") fun getAllStudents(): List<Student> ❖ Use query methods to query data from your app's database, or when you need to perform more complex inserts, updates, and deletes.
RoomDatabase. ❖ It is annotated with @Database. ❖ It holds the database. It defines the database configurations, and serves as main access point to the app’s persistent data. ➢ It contains entities(tables) associated with the database and the database version number.