问题
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?
回答1:
Creating an action from DetailsFragment to DetailsFragment and navigating to this action worked as mentioned by Alex
回答2:
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.
来源:https://stackoverflow.com/questions/51548604/navigation-architecture-component-how-to-refresh-a-fragment