Why there’s a separate MutableLiveData subclass of LiveData?

In LiveData – Android Developer Documentation, you can see that for LiveData, setValue() & postValue() methods are not public. Whereas, in MutableLiveData – Android Developer Documentation, you can see that, MutableLiveData extends LiveData internally and also the two magic methods of LiveData is publicly available in this and they are setValue() & postValue(). setValue(): set … Read more

Room – LiveData observer does not trigger when database is updated

I had a similar problem using Dagger 2 that was caused by having different instances of the Dao, one for updating/inserting data, and a different instance providing the LiveData for observing. Once I configured Dagger to manage a singleton instance of the Dao, then I could insert data in the background (in my case in … Read more

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

You don’t really need a ViewPager to work with BottomNavigation and the new Navigation architecture component. I have been working in a sample app that uses exactly the two, see here. The basic concept is this, you have the main activity that will host the BottomNavigationView and that is the Navigation host for your navigation … Read more

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

It’s not a problem in your case, but for others, this error can occur if you have @Ignore params in your primary constructor, i.e. Room expects to have either: parameterless constructor or constructor with all fields not marked with @Ignore for example: @Entity(tableName = “movies”) data class MovieKt( @PrimaryKey var id : Int, var title: … Read more

LiveData prevent receive the last value when start observing

I`m using this EventWraper class from Google Samples inside MutableLiveData /** * Used as a wrapper for data that is exposed via a LiveData that represents an event. */ public class Event<T> { private T mContent; private boolean hasBeenHandled = false; public Event( T content) { if (content == null) { throw new IllegalArgumentException(“null values … Read more

Android Room: Insert relation entities using Room

You can do this by changing your Dao from an interface to an abstract class. @Dao public abstract class UserDao { public void insertPetsForUser(User user, List<Pet> pets){ for(Pet pet : pets){ pet.setUserId(user.getId()); } _insertAll(pets); } @Insert abstract void _insertAll(List<Pet> pets); //this could go in a PetDao instead… @Insert public abstract void insertUser(User user); @Query(“SELECT * … Read more

AndroidViewModel vs ViewModel

AndroidViewModel provides Application context If you need to use context inside your Viewmodel you should use AndroidViewModel (AVM), because it contains the application context. To retrieve the context call getApplication(), otherwise use the regular ViewModel (VM). AndroidViewModel has application context. We all know having static context instance is evil as it can cause memory leaks!! … Read more

Android Persistence room: “Cannot figure out how to read this field from a cursor”

Document is really confusing. Try with just below classes: 1) User Entity: @Entity public class User { @PrimaryKey public int id; // User id } 2) Pet Entity: @Entity public class Pet { @PrimaryKey public int id; // Pet id public int userId; // User id public String name; } 3) UserWithPets POJO: // Note: … Read more

IllegalStateException: Link does not have a NavController set

Officially recommended solution Currently using the FragmentContainerView is not very friendly, you have to access it from the supportFragmentManager: val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController In my xml my FragmentContainerView looks like this: <androidx.fragment.app.FragmentContainerView android:id=”@+id/nav_host_fragment” android:name=”androidx.navigation.fragment.NavHostFragment” android:layout_width=”0dp” android:layout_height=”0dp” app:defaultNavHost=”true” app:layout_constraintBottom_toBottomOf=”parent” app:layout_constraintLeft_toLeftOf=”parent” app:layout_constraintRight_toRightOf=”parent” app:layout_constraintTop_toBottomOf=”parent” app:navGraph=”@navigation/nav_graph” /> This has been tested with androidx … Read more