片段中的伴侣对象与共享偏好

发布于 2025-02-04 13:47:43 字数 589 浏览 1 评论 0原文

有时,要使用来自其他片段的数据,我使用伴随对象如下所示,

class ExampleFragment : Fragment()  {
[...]

companion object {
   val myValueToShare = 20f
   }
}

在其他片段中,我使用此变量为:

IE

ExampleFragment.myValueToShare = 10f

还可以吗? IE使用共享首选项更好。 上面的哪些方法更轻,为什么?

https://blog.mindorks.com/companion-object-in-kotlin#:~: text = So; %20-持续

Sometimes to use data from other fragment I use companion object as below

class ExampleFragment : Fragment()  {
[...]

companion object {
   val myValueToShare = 20f
   }
}

in other fragment I use this variable as:

i.e.

ExampleFragment.myValueToShare = 10f

Is it alright? Is it better to i.e. use shared preferences.
Which methods from above are more light and why?

https://blog.mindorks.com/companion-object-in-kotlin#:~:text=So%2C%20by%20declaring%20the%20companion,front%20of%20the%20object%20declaration.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文