简体   繁体   English

在片段中将数据放在额外的地方是错误的吗?

[英]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 从活动/片段来回传递数据会有一些限制和问题,例如:

  • To pass a complex object, you'd need to use a serializable marked with a key.要传递复杂的 object,您需要使用标有键的序列化对象。
  • 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()方法,以便它可以清理资源。

Here is a code lab that you'd check. 是您要检查的代码实验室。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ScrollView在显示片段的同时显示额外的空间 - ScrollView showing extra space while showing fragment 如何为每个片段放置特定数据 - How to put specific data for each fragment 实施后如何将片段中的数据放入活动中 - How to put data in the fragment after the implementation into an activity NPE,同时将数据提取到Fragment中 - NPE while fetching data into Fragment FragmentPagerAdapter 在数据更改后显示错误的片段 - FragmentPagerAdapter shows wrong fragment after data change [Android]将数据从片段捆绑到另一个片段时出错 - [Android]Wrong when bundle data from fragment to another fragment Android存储库模式:如何将额外数据传递给Fragment / Activity - Android repository pattern: how pass extra data to Fragment/Activity 如何通过活动将额外数据从一个片段传输到另一个片段 - how to transfer extra data from one fragment to another through activity Android-检查数组是否具有此值,获取位置并发送多余的意图到下一个活动片段 - Android - Check whether the array has this value , get the position and send intent put extra to next activity fragment 如何将多余的数据传递给片段类,以及如何将这些数据分成片段? - How can I pass extra data to fragment class and how to get that data in fragment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM