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
279
votes
10 answers

What is AndroidX?

I am reading about a room library of Android. I see they changed package android to androidx. I did not understand that. Can someone explain, please? implementation "androidx.room:room-runtime:$room_version" annotationProcessor…
222
votes
11 answers

Room persistance library. Delete all

How can I delete all entries on specific table using Room Persistence Library? I need to drop table, but I cannot to find any information how to do this. Only when database is migrating or to load all entries and delete them :)
Sirelon
  • 4,236
  • 3
  • 17
  • 26
213
votes
9 answers

When to use RxJava in Android and when to use LiveData from Android Architectural Components?

I am not getting the reason to use RxJava in Android and LiveData from Android Architectural Components.It would be really helpful if the usecases and differences between the both are explained along with sample example in the form of code which…
202
votes
25 answers

ViewModelProviders is deprecated in 1.1.0

Looking at the Google docs for ViewModel, they show the below sample code on how to get a ViewModel: val model = ViewModelProviders.of(this).get(MyViewModel::class.java) When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there…
202
votes
3 answers

AndroidViewModel vs ViewModel

With the introduction of the Android Architecture Components library, several new classes were introduced, including AndroidViewModel and ViewModel. However, I'm having trouble figuring out the difference between these two classes. The documentation…
184
votes
13 answers

How to clear navigation Stack after navigating to another fragment in Android

I am using The new Navigation Architecture Component in android and I am stuck in clearing the navigation stack after moving to a new fragment. Example: I am in the loginFragment and I want this fragment to be cleared from the stack when I…
127
votes
14 answers

Cannot resolve symbol ViewModelProviders on AppCompatActivity

Hey I'm trying to get my ViewModel working, but no luck so far. Android Studio shows error Cannot resolve symbol 'ViewModelProviders'. Every other question I found on this topic was correcting extends Activity to extends AppCompatActivity, but I…
Sheler
  • 3,699
  • 3
  • 10
  • 20
118
votes
10 answers

Android Room Persistence Library: Upsert

Android's Room persistence library graciously includes the @Insert and @Update annotations that work for objects or collections. I however have a use case (push notifications containing a model) that would require an UPSERT as the data may or may…
Tunji_D
  • 3,199
  • 3
  • 23
  • 31
104
votes
7 answers

Observing LiveData from ViewModel

I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in…
102
votes
5 answers

How to update LiveData of a ViewModel from background service and Update UI

Recently I am exploring Android Architecture, that has been introduced recently by google. From the Documentation I have found this: public class MyViewModel extends ViewModel { private MutableLiveData> users; public…
100
votes
3 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…
99
votes
6 answers

Android Room: Insert relation entities using Room

I've added one to many relationship in Room using Relation. I referred to this post to write the following code for relation in Room. The post tells how to read the values from the database but storing the entities into the database resulted in…
Akshay Chordiya
  • 4,351
  • 2
  • 34
  • 51
87
votes
28 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 :…
83
votes
28 answers

Cannot create an instance of class ViewModel

I am trying to write a sample app using Android architecture components and but even after trying for days I could not get it to work. It gives me the above exception. Lifecycle owner:- public class MainActivity extends LifecycleActivity { …
Parag Kadam
  • 2,659
  • 4
  • 18
  • 39
81
votes
13 answers

IllegalStateException: Link does not have a NavController set

I'm using Android Navigation Component for Navigation. I have a LoginFragment which has a button to transition to SignUpFragment. On clicking the button I'm getting this error. java.lang.IllegalStateException: View…
1
2 3
99 100