Navigation Architecture component- How to refresh a fragment?

落花浮王杯 提交于 2020-12-08 05:57:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!