Questions tagged [android-architecture-components]

A new collection of libraries that help you design robust, testable, and maintainable Android apps. Start with classes for managing your UI component lifecycle and handling data persistence.

Architecture Components compromises of below components:

Handling lifecycles Create a UI that automatically responds to lifecycle events.

LiveData Build data objects that notify views when the underlying database changes.

ViewModel Store UI related data that isn't destroyed on app rotations.

Room Access your data with the power of SQLite and safety of in-app objects.

Data binding Useful to bind data directly through layouts xml file, so no findViewById()anymore.

Navigation Handles navigating between your app's destinations.

1709 questions
108
votes
14 answers

What is the difference between map() and switchMap() methods?

What is the difference between those 2 methods of the LiveData class? The official doc and tutorial are pretty vague on that. In the map() method the first parameter called source but in the switchMap() it called trigger. What's the rationale behind…
108
votes
34 answers

Room Persistence: Error:Entities and Pojos must have a usable public constructor

I'm converting a project to Kotlin and I'm trying to make my model (which is also my entity) a data class I intend to use Moshi to convert the JSON responses from the API @Entity(tableName = "movies") data class MovieKt( @PrimaryKey var id :…
106
votes
4 answers

Why there's a separate MutableLiveData subclass of LiveData?

It looks like MutableLiveData differs from LiveData only by making the setValue() and postValue() methods public, whereas in LiveData they are protected. What are some reasons to make a separate class for this change and not simply define those…
103
votes
12 answers

Manually clearing an Android ViewModel?

Edit: This question is a bit out of date now that Google has given us the ability to scope ViewModel to navigation graphs. The better approach (rather than trying to clear activity-scoped models) would be to create specific navigation graphs for the…
103
votes
6 answers

How and where to use Transformations.switchMap?

In recent Android Architecture Components library released by Google, we have two static functions in the Transformations class. While the map function is straight forward and easily understandable, I am finding it hard to properly understand the…
102
votes
24 answers

Android ViewModel has no zero argument constructor

I am following this documentation to learn about LiveData and ViewModel. In the doc, the ViewModel class has constructor as such, public class UserModel extends ViewModel { private MutableLiveData user; @Inject…
Prabin Timsina
  • 2,131
  • 3
  • 16
  • 27
96
votes
12 answers

Navigation Architecture Component- Passing argument data to the startDestination

I have an activity A that start activity B passing to it some intent data. Activity B host a navigation graph from the new Navigation Architecture Component.I want to pass that intent data to the startDestination fragment as argument how to do that?
96
votes
14 answers

Why LiveData observer is being triggered twice for a newly attached observer

My understanding on LiveData is that, it will trigger observer on the current state change of data, and not a series of history state change of data. Currently, I have a MainFragment, which perform Room write operation, to change non-trashed data,…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
89
votes
6 answers

BoundService + LiveData + ViewModel best practice in new Android recommended architecture

I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach. I did a lot of research,…
81
votes
8 answers

Add (not replace) fragment with navigation architecture component

I have an activity with a product list fragment and many other fragments and I am trying to use architecture component navigation controller. The problem is: it replaces the (start destination) product list fragment and I don't want the list to be…
81
votes
14 answers

Android Jetpack Navigation, BottomNavigationView with Youtube or Instagram like proper back navigation (fragment back stack)?

Android Jetpack Navigation, BottomNavigationView with auto fragment back stack on back button click? What I wanted, after choosing multiple tabs one after another by user and user click on back button app must redirect to the last page he/she…
81
votes
4 answers

When should we use android.arch.lifecycle:compiler (or android.arch.lifecycle:common-java8)?

Currently, we are using LiveData, ViewModel and Room in our project. We are using Java 8. We use the following in build.gradle // ViewModel and LiveData implementation "android.arch.lifecycle:extensions:1.1.1" // Room (use 1.1.0-beta1 for latest…
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875
80
votes
7 answers

How to handle error states with LiveData?

The new LiveData can be used as a replacement for RxJava's observables in some scenarios. However, unlike Observable, LiveData has no callback for errors. My question is: How should I handle errors in LiveData, e.g. when it's backed by some network…
Kirill Rakhman
  • 42,195
  • 18
  • 124
  • 148
79
votes
6 answers

New navigation component from arch with nested navigation graph

I have one case and wish to implement it by arch navigation component. For example I have 2 Nav Graphs (main and nested). Can I call main graph from nested and how?
77
votes
11 answers

How to clear LiveData stored value?

According to LiveData documentation: The LiveData class provides the following advantages: ... 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…
Kamer358
  • 2,430
  • 2
  • 16
  • 17