Navigation Architecture component- How to refresh a fragment? - android

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.

Related

is there any method that can remove fragment's all data by navigate another fragment on Navigation drawer activity?

I need to remove all data of Home Fragment while navigation to Gallery Fragment in Navigation Drawer activity.
My Problem is that I have to delete some value in gallery fragment but that value is loaded in home fragment. By deleting value on Gallery Fragment on a null object reference error comes in Home Fragment.
So my question is that how I can remove loaded data from Home Fragment while navigation to Gallery Fragment.
Kind of hard to answer without knowing what exactly you use for Navigation
If you don't use something like MVVM, you probably should. Navigation Component is also good.
If you are a beginner, the easiest option is to edit the method that gets called when navigating away and just set your variables to null (if within the same Fragment).
Lifecycle methods like onStop() get called in other cases too so I wouldn't use those.
If the Fragment that needs stuff deleted isn't the one executing the code, you should probably have the data outside of a Fragment (e.g. Repository, you can delete the data from it anywhere)

How do I not refresh the list when we use navigation?

I use Android jitpack navigation in the project.
In the first fragment, I show the list in recyclerview.
When I click on the item, I show the second fragment.
But when I go back to the first fragment, recyclerview refreshes the old list.
How do I not refresh the list?
You should use ViewModel to keep list through navigation. Provide the list to your fragment using LiveData and observe it in the fragment's onViewCreated() method

Save Frgament state

I have an activity that includes two fragments, I can navigate between the frgaments by bottom navigation menu. In both of them I have a reyclerView with image and text (data is retrieving by FireBase database).
I have the following code in MainActivity (using switch) that replaces the fragment by using navigation menu. (That is why it recreates the whole fragment every time and retrieves the data again as I found out).
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_space, selectedFragment).commit();
I would like to save the state of each fragment (and allow user in the future manually refresh recyclerView) but so far I failed. How can I make the fragment not be recreated?
Thank you in advance.
Use ViewPager with disabled swipe.

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 inside a fragment in android

In my application I am making use of navigation drawer which is in the MainActivity and this navigation drawer has say 5 fragments. I am not maintaining any backstack of these fragments.
Now, the first fragment has one button which when clicked pushes a fragment (which I call an inner fragment). Here, I am maintaining a backstack because I want to get back to the first fragment from the inner fragment.
Now, I have a requirement in which I want to navigate from an activityA to the inner fragment.
Is this possible?
One way that I have thought of is to have the push code inside the first fragments create method (and make this conditional).
But I don't think its an appropriate way. Any suggestions would be helpful.

Categories

Resources