17

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?

Mycoola
  • 1,135
  • 1
  • 8
  • 29
Sammys
  • 1,443
  • 5
  • 14
  • 21
  • 1
    You should notify the recyclerview adapter rather than replacing a fragment if that is the only view that needs refreshed – OneCricketeer Jul 26 '18 at 23:06
  • Sounds like you're asking about - https://developer.android.com/training/basics/fragments/communicating – OneCricketeer Jul 26 '18 at 23:07
  • I need to refresh the contents of the DetailsFragment and the recylerview on the bottom, based on which item is selected from the bottom recyclerview – Sammys Jul 26 '18 at 23:08
  • You can add an onclick interface into the recyclerview adapter, then within the callback method, you would implement the other transitions of the screen. For starters - https://antonioleiva.com/recyclerview-listener/ – OneCricketeer Jul 26 '18 at 23:26
  • 5
    Create action from DetailsFragment to DetailsFragment(you can do this from navigation editor), and navigate to this action on item click – Alex Aug 01 '18 at 08:14

2 Answers2

19

Creating an action from DetailsFragment to DetailsFragment and navigating to this action worked as mentioned by Alex

Sammys
  • 1,443
  • 5
  • 14
  • 21
0

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.

Alberto M
  • 1,608
  • 1
  • 18
  • 42