[英]Is it wrong to put data in extra while in fragment?
I have one Activity and want to share data between that Activity and fragments.我有一个 Activity 并想在该 Activity 和片段之间共享数据。 I put data in extra while in a fragment and put also other data in it.我将数据放在片段中,同时将其他数据放入其中。 That way I Have a shared Bundle across my application.这样我就可以在我的应用程序中共享一个 Bundle。 I only see examples of passing a Bundle to an Intent but its also possible to change that data while in another fragment.我只看到将 Bundle 传递给 Intent 的示例,但也可以在另一个片段中更改该数据。 This does not break with the self-containment of fragments.这不会破坏片段的自包含性。 I dont put them in some method in activity because then you will have to cast the activity.我没有将它们放在活动中的某种方法中,因为那样你将不得不执行活动。 Can anybody tell me its right to do?谁能告诉我这样做的权利? I know about shared pref but I dont want a file based solution.我知道共享首选项,但我不想要基于文件的解决方案。 I know about passing parameters with newInStance but I also need to save data back in fragments.我知道如何使用 newInStance 传递参数,但我还需要将数据保存回片段中。 passing parameters is only forward not shared.传递参数仅转发不共享。
Passing data from activity/fragment back & forth using Bundles would have some limitations and issues for instance:使用 Bundle 从活动/片段来回传递数据会有一些限制和问题,例如:
Keeping shared keys in different parts can lead to runtime errors or data loss if keys are wrong.如果密钥错误,将共享密钥保存在不同的部分可能会导致运行时错误或数据丢失。
Serialized objects are not recommended, but it can be solved with Parcelables.不推荐序列化对象,但是可以用Parcelables解决。 But maintaining that is not that easy for complex objects check here , and you would need to customize that for different types of objects.但是对于复杂的对象来说,维护它并不那么容易,你需要为不同类型的对象定制它。
Still you don't share data among different fragments but they're just transported;您仍然不会在不同的片段之间共享数据,但它们只是被传输; and need to be transported over and over again when you go to a new part of your app.当您 go 到应用程序的新部分时,需要一遍又一遍地传输。
Not guaranteed to keep the data if the activity is recreated.如果重新创建活动,则不能保证保留数据。
Instead of that you'd use ViewModels through MVVM structure where:相反,您将通过 MVVM 结构使用ViewModels ,其中:
Data can be shared on different levels of lifecycle scopes;数据可以在不同级别的生命周期范围内共享; this means that if the ViewModel is instantiated through ViewModelProvider
in activity;这意味着如果ViewModelProvider
实例化; then it can be accessed in any part of the activity, or underlying fragments.然后它可以在活动的任何部分或底层片段中访问。 And if you want to keep only data shared between any fragment and its underlying fragments;而且,如果您只想保留在任何片段及其基础片段之间共享的数据; you'd bound the ViewModel
instantiation to that fragment instead.您改为将ViewModel
实例化绑定到该片段。
ViewModel is instantiated once in the owner, and accessed in the subordinate fragments with no re-instantiation. ViewModel 在所有者中实例化一次,并在从属片段中访问而无需重新实例化。
If the activity is re-created, it receives the same ViewModel
instance that was created by the owner, and the view's data won't be lost.如果重新创建活动,它会收到所有者创建的相同ViewModel
实例,并且视图的数据不会丢失。
When the owner activity/fragment is finished, the framework automatically calls the ViewModel's onCleared()
method so that it can clean up the resources.当所有者活动/片段完成时,框架会自动调用 ViewModel 的onCleared()
方法,以便它可以清理资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.