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.

1647 questions
79
votes
11 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…
78
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…
77
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…
73
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…
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
13 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
  • 49,649
  • 117
  • 410
  • 768
72
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
  • 35,458
  • 17
  • 110
  • 133
70
votes
5 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,…
68
votes
21 answers

Room "Not sure how to convert a Cursor to this method's return type": which method?

Error:Not sure how to convert a Cursor to this method's return type Error:Execution failed for task ':app:compileDebugJavaWithJavac'. Compilation failed; see the compiler error output for details. Using Room I'm getting this error and I'd like to…
67
votes
7 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,322
  • 2
  • 12
  • 16
66
votes
17 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
  • 1,686
  • 2
  • 14
  • 24
65
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…
64
votes
7 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…
63
votes
7 answers

How to get a result from fragment using Navigation Architecture Component?

Let's say that we have two fragments: MainFragment and SelectionFragment. The second one is build for selecting some object, e.g. an integer. There are different approaches in receiving result from this second fragment like callbacks, buses etc.…
62
votes
9 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?