Proguard causing runtime exception with Android Navigation Component

I know that Proguard and R8 should be keeping all the children of library classes but in this case, the fragment class seems to be missing. This keep rule solved my issue but technically we should not need this rule at all! -keep class * extends android.support.v4.app.Fragment{} If you are using AndroidX, then use this … 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”

Is it possible to set startDestination conditionally using Android Navigation Architecture Component(Android Jetpack)?

Finally, I got a solution to my query… Put below code in onCreate() method of Activity. Kotlin code val navHostFragment = (supportFragmentManager.findFragmentById(R.id.home_nav_fragment) as NavHostFragment) val inflater = navHostFragment.navController.navInflater val graph = inflater.inflate(R.navigation.nav_main) //graph.addArgument(“argument”, NavArgument) graph.setStartDestination(R.id.fragment1) //or //graph.setStartDestination(R.id.fragment2) navHostFragment.navController.graph = graph Java code NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.home_nav_fragment); // Hostfragment NavInflater inflater = navHostFragment.getNavController().getNavInflater(); NavGraph graph … Read more

How to change start destination of a navigation graph programmatically?

UPDATE: When you have nav graph like this: <fragment android:id=”@+id/firstFragment” android:name=”com.appname.package.FirstFragment” > <action android:id=”@+id/action_firstFragment_to_secondFragment” app:destination=”@id/secondFragment” /> </fragment> <fragment android:id=”@+id/secondFragment” android:name=”com.appname.package.SecondFragment”/> And you want to navigate to the second fragment and make it root of your graph, specify the next NavOptions: NavOptions navOptions = new NavOptions.Builder() .setPopUpTo(R.id.firstFragment, true) .build(); And use them for the navigation: Navigation.findNavController(view).navigate(R.id.action_firstFragment_to_secondFragment, … 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

Fragments destroyed / recreated with Jetpack’s Android Navigation components

Ian Lake from google replied me that we can store the view in a variable and instead of inflating a new layout, just return the instance of pre-stored view on onCreateView() Source: https://twitter.com/ianhlake/status/1103522856535638016 Leakcanary may show this as leak but its false positive..

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

IllegalArgumentException: navigation destination xxx is unknown to this NavController

In my case, if the user clicks the same view twice very very quickly, this crash will occur. So you need to implement some sort of logic to prevent multiple quick clicks… Which is very annoying, but it appears to be necessary. You can read up more on preventing this here: Android Preventing Double Click … Read more