简体   繁体   English

在Android中的片段之间共享数据

[英]Share data between fragments in android

What is the best solution to deal with an "AdapterA of a RecyclerViewA which is inside a FragmentA" that want to use data from another "AdapterB of a RecyclerViewB which is inside a FragmentB" ? 想要使用“ FragmentA内的RecyclerViewA的AdapterA 要使用另一个“ FragmentB内的RecyclerViewB的AdapterB”中的数据的最佳解决方案什么?

在此处输入图片说明

I am stuck,i tried to : 我被困住了,我试图:
1- Make the data static in the adapter (no garbage collector) 1-使数据在适配器中静态(无垃圾收集器)
2- Duplicate data that i need (waste of memory) 2-我需要重复的数据(浪费内存)
(it works but may be there is a better solution) (它可以工作,但是可能有更好的解决方案)

Thank you in advance. 先感谢您。 (plz ask for details if you need) (请询问是否需要详细信息

If you, in MainActivity, create a new instance of a class where you contain whatever you want both fragments to access. 如果您在MainActivity中创建一个类的新实例,其中包含您希望两个片段都访问的内容。 If you implement Serializable or Parcelable, you can also send it to each fragment using Bundle/Intent. 如果实现Serializable或Parcelable,也可以使用Bundle / Intent将其发送到每个片段。

Then, as you have the same instance in two different fragments, if you edit data in fragment X, fragment Y will be able to access it. 然后,由于您在两个不同的片段中具有相同的实例,因此,如果您在片段X中编辑数据,则片段Y将能够访问它。

See this: 看到这个:

   |---MainActivity---|
   |         |        |
   V         V        V
 Frag A <->Data <->Frag B

MainActivity creates a new class(data) which it sends to each of the fragments. MainActivity创建一个新的类(数据),并将其发送到每个片段。 The fragments can update the data in the class. 片段可以更新类中的数据。 Please note, that you have to use class if any given data type isn't supported. 请注意,如果不支持任何给定的数据类型,则必须使用类。

If you don't feel like using Serializable or parcelable, if possible, send MainActivity as an instance to each of the fragments. 如果您不想使用Serializable或parcelable,则将MainActivity作为实例发送给每个片段。 From each fragment you then get the MainActivity instance and find the data you need. 然后,从每个片段中获取MainActivity实例并找到所需的数据。

If you can't pass MainActivity to either of the fragments, and cannot use Serializable/Parcelable and the data type isn't supported by bundle.putExtra or intent.putExtra, you have to use a static import. 如果您不能将MainActivity传递给任何一个片段,并且不能使用Serializable / Parcelable并且bundle.putExtra或intent.putExtra不支持数据类型,则必须使用静态导入。

Those are your only options. 这些是您唯一的选择。

Alternatively, you can create a class that extends "Application". 或者,您可以创建扩展“应用程序”的类。 Then you write: 然后你写:

MyApplicationClass mac = (MyApplicationClass) getApplicationContext();

Then you access the data in the application-extending class(here: the mac instance) 然后,您可以访问应用程序扩展类中的数据(此处为mac实例)

Final words 最后的话

If you do not want to use static instance, send a parcelable/serializable class with the contents, or use a class extending Application, there is no way you can transfer the data(considering you are using a HashMap which you claim cannot be sent by Intent or Bundle). 如果您不想使用静态实例,发送包含内容的可打包/可序列化类或使用扩展应用程序的类,则无法传输数据(假设您使用的HashMap声称无法通过以下方式发送)意向或捆绑)。 If you had a data type or class that was possible to send using Intent or Bundle, you wouldn't have to use static instance or parcelable/serializable class. 如果您具有可以使用Intent或Bundle发送的数据类型或类,则不必使用静态实例或可打包/可序列化的类。 But with the position you are in, I have presented all the options you have. 但是,根据您所处的位置,我已经介绍了您拥有的所有选择。 There are basically no other ways than using a class containing the hashmap, using a static instance or utilizing the Application class. 除了使用包含哈希图的类,使用静态实例或使用Application类之外,基本上没有其他方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM