Android,在某些更改时通知Backstack片段 [英] Android, notify backstack fragments on some change

查看:86
本文介绍了Android,在某些更改时通知Backstack片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发生某些更改时通知后端堆栈中的所有片段的最佳实践是什么?

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

我尝试使用EventBus,每个Fragment都订阅该事件,然后在发生更改时,主活动将发布事件发送给所有订阅者.

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.

onDestroy我取消订阅被破坏的片段.

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.

推荐答案

一种可行的解决方案是将一些数据放入共享的首选项中,并在片段onResume中读取.

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.

用于远程获取数据

您可以将数据放入仅负责保存数据的单例类.请记住,在android系统中,在某些极限情况下,单例可能会被破坏,因此当您的片段回到前台时,检查单例是否为空,并最终重复您的提取调用

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

您可以在此处

这篇关于Android,在某些更改时通知Backstack片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆