Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation Component , add or replace fragments or other things like hide show fragments

how can we specify replace or add fragment in Navigation component?

NavHostFragment.findNavController(this).navigate(R.id.action_splashFragment_to_categoriesFragment)
like image 257
Mohammad Hadi Avatar asked Nov 06 '18 10:11

Mohammad Hadi


People also ask

How do you adding removing and replacing fragments?

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();

What is navigation component?

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.


1 Answers

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

like image 114
Ramakrishna Joshi Avatar answered Nov 08 '22 08:11

Ramakrishna Joshi