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
75
votes
8 answers

Sharing data between fragments using new architecture component ViewModel

On Last Google IO, Google released a preview of some new arch components, one of which, ViewModel. In the docs google shows one of the possible uses for this component: It is very common that two or more fragments in an activity need to communicate…
72
votes
4 answers

Paging library - Boundary callback for network + db with API taking page and size

Short question: What is the correct way to handle database + network on the Paging library from Architecture components, using an API that uses page + size to load a new page and the BoundaryCallback class? Research and explanation Currently the…
72
votes
10 answers

Navigation Architecture Component - Dialog Fragments

Is it possible to use the new Navigation Architecture Component with DialogFragment? Do I have to create a custom Navigator? I would love to use them with the new features in my navigation graph.
70
votes
6 answers

Unit testing Room and LiveData

I'm currently developing an app using the newly Android Architecture Components. Specifically, I'm implementing a Room Database that returns a LiveData object on one of its queries. Insertion and querying work as expected, however I have an issue…
68
votes
3 answers

Navigation Preview unavailable in Android Studio 3.2 Preview

I am interested to try the Navigation graph showed in the Android Studio. But I got the preview unavailable after I import the google sample I used the Android Studio 3.2 Preview Canary 16
68
votes
5 answers

Return type for Android Room joins

Let's say I want to do an INNER JOIN between two entities Foo and Bar: @Query("SELECT * FROM Foo INNER JOIN Bar ON Foo.bar = Bar.id") List findAllFooAndBar(); Is it possible to force a return type like this? public class FooAndBar { …
pqvst
  • 4,344
  • 6
  • 33
  • 42
66
votes
6 answers

How can I represent a "many to many" relation with Android Room when column names are same?

How can I represent a "many to many" relation with Room? My column names are also the same. e.g. I have Guest and Reservation. Reservation can have many Guest's and a Guest can be part of many Reservations. Here are my entity definitions: @Entity…
bond
  • 11,236
  • 7
  • 48
  • 62
65
votes
4 answers

add unique constraint in room database to multiple column

I have one entity in room @Entity(foreignKeys ={ @ForeignKey(entity = Label.class, parentColumns = "_id", childColumns = "labelId", onDelete = CASCADE), @ForeignKey(entity = Task.class, parentColumns = "_id", childColumns = "taskId",…
64
votes
4 answers

Why does viewModelScope.launch run on the main thread by default

While I was learning coroutines and how to properly use them in an android app I found something I was surprised about. When launching a coroutine using viewModelScope.launch { } and setting a breakpoint inside the launch lambda I noticed my app…
63
votes
2 answers

How to create a Worker with parameters for in WorkManager for Android?

Android architecture has a new components WorkManager. From the example, class CompressWorker(context : Context, params : WorkerParameters) : Worker(context, params) { override fun doWork(): Result { // Do the work here--in this…
62
votes
8 answers

When to use MutableLiveData and LiveData

when to use MutableLiveData and LiveData means the area of using the methods : MutableLiveData getUser() { if (userMutableLiveData == null) { userMutableLiveData = new MutableLiveData<>(); } return…
59
votes
10 answers

How I can retrieve current fragment in NavHostFragment?

I tried to find a method in the new Navigation components but I didn't find anything about that. I have the current destination with : mainHostFragment.findNavController().currentDestination But I can't get any reference to the displayed fragment.
57
votes
13 answers

How to combine two live data one after the other?

I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show error message or create new user... I am trying to…
56
votes
9 answers

Asynchronous Worker in Android WorkManager

Google recently announced new WorkManager architecture component. It makes it easy to schedule synchronous work by implementing doWork() in Worker class, but what if I want to do some asynchronous work in the background? For example, I want to make…
56
votes
16 answers

LiveData prevent receive the last value when start observing

Is it possible to prevent LiveData receive the last value when start observing? I am considering to use LiveData as events. For example events like show message, a navigation event or a dialog trigger, similar to EventBus. The problem related to…