how can we specify replace or add fragment in Navigation component?
NavHostFragment.findNavController(this).navigate(R.id.action_splashFragment_to_categoriesFragment)
Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();
The Navigation Component is made up of three major parts:This includes all of the locations in your app, referred to as destinations, as well as the possible paths a user could take through your app. NavHostFragment (Layout XML view) — This is a unique widget that you can include in your layout.
Jetpack Navigation Component supports replacing fragments as of now.
There is no support for adding fragments.
Workaround to retain api data in fragment when back button is pressed is to either save the data (Fragment viewstate) in a ViewModel and make a check in onViewCreated() or to use Full Screen DialogFragments. For dialog fragment check this https://gist.github.com/utkukutlu/bd2e37253e7184179d923321e3ea72c1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With