{"id":21084899,"url":"https://github.com/yuvraj24/archcomponents","last_synced_at":"2026-05-20T02:53:06.345Z","repository":{"id":123139923,"uuid":"126939024","full_name":"yuvraj24/ArchComponents","owner":"yuvraj24","description":"The repository covers the concepts regarding ViewModel, LiveData, LifeCycle along with Retrofit \u0026 RxJava in a Kotlin","archived":false,"fork":false,"pushed_at":"2018-04-14T20:00:55.000Z","size":569,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-21T00:15:42.710Z","etag":null,"topics":["android","architecture-components","kotlin","retrofit","rxandroid","rxjava"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yuvraj24.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-27T06:24:35.000Z","updated_at":"2020-07-03T22:02:43.000Z","dependencies_parsed_at":"2023-04-30T12:31:03.337Z","dependency_job_id":null,"html_url":"https://github.com/yuvraj24/ArchComponents","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuvraj24%2FArchComponents","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuvraj24%2FArchComponents/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuvraj24%2FArchComponents/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yuvraj24%2FArchComponents/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yuvraj24","download_url":"https://codeload.github.com/yuvraj24/ArchComponents/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243526953,"owners_count":20305115,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","architecture-components","kotlin","retrofit","rxandroid","rxjava"],"created_at":"2024-11-19T20:27:55.068Z","updated_at":"2026-05-20T02:53:01.315Z","avatar_url":"https://github.com/yuvraj24.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Android Architecture Components\n\n![Alt text](https://github.com/yuvraj24/ArchComponents/blob/master/image/kotlin-arch_android.png)\n\nCheck out the Demo app from the Play Store link below.\n\n\u003ca href='https://play.google.com/store/apps/details?id=com.arch.components'\u003e\u003cimg alt='Get it on Google Play' src='https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png' height=\"80px\"/\u003e\u003c/a\u003e\n\nA typical Android app is constructed out of multiple app components, including activities, fragments, services, content providers and broadcast receivers. Android app needs to be much more flexible as the user weaves their way through the different apps on their device, constantly switching flows and tasks. This app-hopping behavior is common, so your app must handle these flows correctly.\n\nKeep in mind that mobile devices are resource constrained, so at any time, the operating system may need to kill some apps to make room for new ones. The point of all this is that your app components can be launched individually and out-of-order, and can be destroyed at anytime by the user or the system. Because app components are ephemeral and their lifecycle (when they are created and destroyed) are not under your control, you should not store any app data or state in your app components and your app components should not depend on each other.\n\nThe new architecture has following components that make it easy. They are also designed to fit together as building blocks.\n\n\u003cimg src='https://cdn-images-1.medium.com/max/1600/1*KnYBBZIDDeg4zVDDEcLw2A.png'/\u003e\n\n# Room\n\nIn new approach, lets tackle the database using Room which a new SQLite object mapping library. To setup a table in room you can define a Plain Old Java Object with annotation @Entity and @PrimaryKey.\n\n```html\n@Entity(tableName = “comments”, foreignKeys = {@ForeignKey(entity = ProductEntity.class, parentColumns = “id”, childColumns = “productId”,onDelete = ForeignKey.CASCADE)}, indices = {@Index(value = “productId”)})\npublic class CommentEntity implements Comment{ @PrimaryKey(autoGenerate = true)\n   private int id;\n   private int productId;\n   private String text;\n   private Date postedAt;\n}\n```\n\nFor each POJO you need to define a Database Access Object (DAO). The annotated method represent the SQLite commands to interact with POJO data.\n\n```html\n@Dao\npublic interface CommentDao {\n   @Query(“SELECT * FROM comments where productId = :productId”)\n   List loadComments(int productId);\n\n   @Insert(onConflict = OnConflictStrategy.REPLACE)\n   void insertAll(List products);\n}\n```\n\nIn above example Room automatically converts POJO object into corresponding database tables and back again.\n\n## LiveData\nLiveData is a data holder class that keeps a value and allows this value to be observed. Unlike a regular observable, LiveData respects the lifecycle of app components, such that the Observer can specify a Lifecycle in which it should observe.\n\nLiveData considers an Observer to be in an active state if the Observer’s Lifecycle is in STARTED or RESUMED state. If the Lifecycle is not in an active state (STARTED or RESUMED), the observer isn’t called even if the value changes. If the Lifecycle is destroyed, the observer is removed automatically. There might be multiple fragments and activities that are observing LiveData instance, and LiveData gracefully manages them such that it connects to the system service only if any of them is visible (that is, active).\n\nThe LiveData class provides the following advantages:\n\n* No memory leaks: Since the Observers are bound to their own Lifecycle objects, they are automatically cleaned when their Lifecycle is destroyed.\n* No crashes due to stopped activities: If the Observer’s Lifecycle is inactive (like an activity in the back stack), they won’t receive change events.\n* Always up to date data: If a Lifecycle starts again (like an activity going back to started state from the back stack) it receives the latest location data (if it didn’t already).\n* Proper configuration change: If an activity or fragment is re-created due to a configuration change (like device rotation), it instantly receives the last available Location data.\n* Sharing Resources: Now we can keep a single instance of class, connect to the system service just once, and properly support all observers in the app.\n* No more manual lifecycle handling: Fragment can observe the data when it wants to, does not worry about being stopped or start observing after being stopped. LiveData automatically manages all of this since the fragment provided its Lifecycle while observing.\n\n## Lifecycle Owners and Lifecycle Observers\nLifecycleOwners are objects with lifecycle like Activity and Fragments. LifecycleObservers observes LifecycleOwners and are notified of lifecycle changes.\n\n```html\nclass TestObserver implements LifecycleObserver{\n   @OnLifecycleEvent(ON_CREATE)\n   void startUp(LifecycleOwner source) {\n   }\n\n   @OnLifecycleEvent(ON_ANY)\n   void onAny(LifecycleOwner source, Event event) {\n   }\n  \n   @OnLifecycleEvent(ON_STOP)\n   void cleanUp(LifecycleOwner source) {\n   }\n}\n```\n\nLifecycle uses two main enumerations to track the lifecycle status for its associated component.\n\n### Event\n\nThe lifecycle events that are dispatched from the framework and the Lifecycle class. These events map to the callback events in activities and fragments.\n\n### State\n\nThe current state of the component tracked by the Lifecycle object.\n\n# ViewModel\n\nThe ViewModel class is designed to store and manage UI-related data so that the data survives configuration changes such as screen rotations.\n\nIt is easier and more efficient to separate out view data ownership from UI controller logic. Lifecycles provides a new class called ViewModel, a helper class for the UI controller which is responsible for preparing the data for the UI. The ViewModel is automatically retained during configuration changes so that the data it holds is immediately available to the next activity or fragment instance.\n\nIf the activity is re-created, it receives the same ViewModel instance that was created by the previous activity. When the owner activity is finished, the Framework calls ViewModel’s onCleared() method so that it can clean up resources.\n\nTo create a ViewModel class, you extend the ViewModel class and then put all the data that is necessary for you UI into this class.\n\n```html \npublic class MyViewModel extends ViewModel {\nprivate MutableLiveData users;\n public LiveData getUsers() {\n   if (users == null) {\n       users = new MutableLiveData();\n       loadUsers();\n   }\n  return users;\n }\n  private void loadUsers() {\n   // do async operation to fetch users\n }\n}\n```\n\nSetup this view model in your activity or fragment like this:\n\n```html \nviewModel.getComments().observe(this,new Observer() {\n   @Override public void onChanged (@Nullable List comments){\n   if (comments != null) {\n       mCommentsAdapter.setCommentsList(comments);\n   }\n }\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuvraj24%2Farchcomponents","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyuvraj24%2Farchcomponents","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyuvraj24%2Farchcomponents/lists"}