How to implement a ViewPager with BottomNavigationView using new Navigation Architecture Component?

UPDATE (15/06/21): Starting from Navigation component version 2.4.0-alpha01 multiple back stacks are supported out of the box. According to documentation if you are using NavigationView or BottomNavigationView together with Navigation component, then multiple back stacks should work without any code changes to previous implementation. As part of this change, the NavigationUI methods of onNavDestinationSelected(), BottomNavigationView.setupWithNavController() … Read more

How to combine two live data one after the other?

You can use my helper method: val profile = MutableLiveData<ProfileData>() val user = MutableLiveData<CurrentUser>() val title = profile.combineWith(user) { profile, user -> “${profile.job} ${user.name}” } fun <T, K, R> LiveData<T>.combineWith( liveData: LiveData<K>, block: (T?, K?) -> R ): LiveData<R> { val result = MediatorLiveData<R>() result.addSource(this) { result.value = block(this.value, liveData.value) } result.addSource(liveData) { result.value = … Read more

How I can retrieve current fragment in NavHostFragment?

Reference to the displayed fragment (AndroidX): java public Fragment getForegroundFragment(){ Fragment navHostFragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment); return navHostFragment == null ? null : navHostFragment.getChildFragmentManager().getFragments().get(0); } kotlin val navHostFragment: Fragment? = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) navHostFragment?.childFragmentManager?.fragments?.get(0) Here nav_host_fragment is an ID of the fragment tag in your activity_main.xml with android:name=”androidx.navigation.fragment.NavHostFragment”

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

I have introduced just one change in your code: noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class); instead of: noteViewModel = ViewModelProviders.of(getActivity()).get(NoteViewModel.class); in Fragment‘s onCreate(Bundle) methods. And now it works seamlessly. In your version you obtained a reference of NoteViewModel common to both Fragments (from Activity). ViewModel had Observer registered in previous Fragment, I think. Therefore LiveData kept reference to … Read more

ViewModelProviders is deprecated in 1.1.0

I use lifecycle-extensions 2.2.0 version: implementation “androidx.lifecycle:lifecycle-extensions:2.2.0” It should work, using ViewModelProvider constructor. // With ViewModelFactory val viewModel = ViewModelProvider(this, YourViewModelFactory).get(YourViewModel::class.java) //Without ViewModelFactory val viewModel = ViewModelProvider(this).get(YourViewModel::class.java) 2020/5/15 Update I found another elegant way to achieve this, Android KTX can help implementation “androidx.fragment:fragment-ktx:1.2.4” val viewmodel: MYViewModel by viewModels() val viewmodel: MYViewModel by viewModels { myFactory … Read more

Return type for Android Room joins

Dao @Query(“SELECT * FROM Foo”) List<FooAndBar> findAllFooAndBar(); Class FooAndBar public class FooAndBar { @Embedded Foo foo; @Relation(parentColumn = “Foo.bar_id”, entityColumn = “Bar.id”) List<Bar> bar; // If we are sure it returns only one entry // Bar bar; //Getter and setter… } This solution seems to work, but I’m not very proud of it. What do … Read more

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

I had a similar issue. Here is my solution. You can use an extra entity (ReservationGuest) which keeps the relation between Guest and Reservation. @Entity data class Guest( @PrimaryKey val id: Long, val name: String, val email: String ) @Entity data class Reservation( @PrimaryKey val id: Long, val table: String ) @Entity data class ReservationGuest( … Read more