Android, notify backstack fragments on some change

戏子无情 提交于 2019-12-11 08:53:49

问题


What is the best practice to notify all the fragments that are in backstack on some change?

I try to use EventBus and every Fragment subscribe to the event, then when change is happening the main activity send post event to all the subscribers.

Only onDestroy I Unsubscribe the destroyed fragment.

I don't like this solution because if there are many fragments in backstack, it can be heavy + lots of listeners simultany.

my application, has infinity drill down, from one fragment you replace to other (and add to backstack) and you can replace again (and add to backstack) and so on..., with no end.


回答1:


A possible solution is to put some data into shared preferences and read it in fragment onResume.

Or you could put that informations in other parts, like configuration servers or external service

Obviously if these fragments belong all to the same activity you can put informations into your activity, then you can read it from that attached activity.

For remote fetched data

You may put your data into a singleton class only responsible to keep data. Keep in mind that in android a singleton could be destroyed in some limit cases, so when your fragment come back in foreground check if the singleton is empty and eventually repeat your fetch call

You can see an example here



来源:https://stackoverflow.com/questions/50059290/android-notify-backstack-fragments-on-some-change

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