How to keep data when navigate to another fragment use navigation direction? - android

I have 2 fragment: fragment1 and fragment2
I'm using Navigation to navigate fragment1 to fragment2:
val action = Fragment1Directions.actionFragment1ToFragment2()
navController.navigate(action)
My fragment1 contain RecyclerView with data change. When I clicked to item in fragment1's RecyclerView will open fragment2. And when I press back button I back to fragment1 with loss data.
I want to keep my data when move to fragment2 and back to fragment1 (fragment2 like popup in web). How can I do it.
Update:
I used View Model to keep LiveData. but when I back from fragment2 to fragment1, fragment1 reloaded. How can I open fragment2 and hold fragment1 (temp open fragment2). I'm using Navigation

Try using livedata to keep track of your data.

I found the solution:
use View Model to keep LiveData
use Dialog destinations for fragment2. Visit https://gist.github.com/utkukutlu/bd2e37253e7184179d923321e3ea72c1 for more detail with Dialog destinations
Learned experience:
Navigation fragment only replace not have add. I want Navigation act like add so I use Dialog destinations
I use Dialog destinations so I have to use View Model to keep LiveData when I update my data in fragment1

Related

How do I get back to the fragment without losing data using Navigation Component?

I have a single activity named MainActivity which holds the FirstFragment.
Using Navigation Components and I already set that the single fragment that can be opened from FirstFragment is SecondFragment. I have also enabled the back button to be able to navigate back from SecondFragment to the FirstFragment. But, when I come back the fields are filled differently than I left.

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)
If you just use findNavController().navigate(R.id.someDestinationId) this kind of equals to fragmentManager.add but if you want the replace behavior and you don't want the user be able to press back and reload the previous activity you should do like this:
findNavController().navigate(R.id.homeFragment, null, NavOptions.Builder()
.setPopUpTo(R.id.loginFragment, true)
.build())
In this case I'm inside LoginFragment and I want to navigate to HomeFragment and I also would like to replace current fragment so the user won't be able to press back and load the LoginFragment again
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

Navigation Architecture component- How to refresh a fragment?

I have a ListFragment that has a recyclerview and onClick on any items opens the DetailsFragment. The details fragment contains another recyclerview on the bottom that shows "MORE ITEMS". Now onClick on any of these items should open the DetailsFragment for that particular item. Basically the fragment needs to be refreshed.
In the past, I would just replace fragment using fragmentManager. How do I go about refreshing the fragment? How can I create an action that points to the same fragment?
Creating an action from DetailsFragment to DetailsFragment and navigating to this action worked as mentioned by Alex
The best way to update a fragment is to use a shared viewmodel between fragments or activity/fragment.
You can read the documentation here https://developer.android.com/topic/libraries/architecture/viewmodel#sharing
Activity or Fragment A is doing some operation, at the end of it updates the viewmodel. Fragment B (that needs to be updated) is observing the same viewmodel and can update the values in the UX.
Creating an action that reloads the fragment is time consuming and not always suitable for each use case.

ListFragment onClick DetailFragment

I have a navigation drawer with few contents. On click selected navigation content i'm adding ListFragment into activity container. When i clicked on the selected ListFragment position i want to show detail of this selected content. What is the better approach:
Replace the MainActivity container(which is ListFragment) by DetailFragment.
New DetailActivity which include the DetailFragment.
Or there is a other approach.
So you essentially need to have a ChildFragment inside your ListFragment
Use getChildFragmentManager() inside your ListFragment for your DetailFragment transactions. This is the best approach if you need to notify anything to your ListFragment from your DetailFragment in future.
So your activity will just have the fragment holder for your ListFragment. Your ListFragment will have the fragment holder for your child fragments, in your case, DetailFragment.
Having a separate DetailActivity for each list view click is good but not the best approach as you will lose your ListActivity context then.
For reference : https://stackoverflow.com/a/17132254/1115353
I believe in the most of cases it's better to replace old fragment with a new one when you are switching between drawer menu items. If you go deeper in already existing fragment on MainActivity you should use "add" method with saving fragments in stack.
If you want to change content from Drawer - it's better to replace fragment.
If you want to add new content above existing, like adding details, you should use "add" method
The second approach is the better, where the Gmail app uses that approach.

Fragment instance in fragment manger is not removing even that fragment got popped up

I have a Fragment with list view. OnItem click of that list view I will replace the fragment's container with the new fragment. The new fragment will have the view pager and fragment state pager adapter. This adapter will create three fragments once. Now, all the five fragments (List fragment, fragment which has the view pager and the three fragments which are created by the fragment state pager adapter) are in the fragment manager. If I came back to the fragment which has list view. All the fragments except the one with list view should be removed from the fragment manager. But, they are not removing.
The problem is, If I click an item in the list view It will create three fragments all the time. And If I do orientation change, the fragment manager is recreating all the fragments. Each time I do orientation change three fragments are getting added to the fragment manager.
Is there any way to remove the fragment associated with view pager when I come back from the view pager fragment.
Without seeing the code I think this is your problem:
Every time your orientation changes, you add a new fragment manager transaction to the backstack. When you pop the backstack, you don't go back to the initial state but to another state where you still have the same fragment visible.
There are multiple ways around it. Choose one:
Don't add the fragment manager transaction to the backstack when you replace the fragments. When you need to go back to the initial fragment, just replace the contents with the fragment that you want. You can always keep a reference to the fragment in the activity if you don't want to re-create the fragment.
Pop the backstack before replacing the fragments and adding another transaction to the backstack. This way you are not accumulating transactions in the backstack. When you pop, the backstack you will always come to the previous state.
Name your fragment transaction so you can pop the backstack with popBackstack("fragmentTransactionName", 0) or popBackStack(transactionId, 0)
See http://developer.android.com/reference/android/app/FragmentManager.html
Hope this helps

Categories

Resources