简体   繁体   English

如何在属于同一容器活动的两个片段之间传递数据?

[英]How can I pass data between two fragments belonging to same container activity?

I've an arraylist in one fragment that I need to pass to another fragment. 我在一个片段中有一个arraylist,需要传递给另一个片段。 Both of these fragments belong to the same container activity. 这两个片段都属于同一容器活动。 This is what I came up with but apparently there's something wrong with it. 这是我想出的,但显然有问题。

This is how I'm sending the data from Fragment1: 这就是我从Fragment1发送数据的方式:

    Bundle b=new Bundle();
    b.putStringArrayList("Brands",allBrands);
    Fragment fragment = new Fragment();
    fragment.setArguments(b);

This is how I'm trying to receive the data at Fragment2: 这就是我试图在Fragment2上接收数据的方式:

 brands = getArguments().getStringArrayList("Brands");

I understand this is not how it is done. 我了解这不是完成方式。 Please help me out. 请帮帮我。 I checked the question on implementing fragmentlistener.This isn't about that. 我检查了有关实现fragmentlistener的问题。

There are few different ways of communicating between fragments. 片段之间通信的方式很少。

1) creating a interface 2) shared preferences 3) sqlite database 1)创建界面2)共享首选项3)sqlite数据库

using 1) you create a common method where the information can be read by both fragments. 使用1)创建一个通用方法,其中两个片段都可以读取信息。

using 2) is great to save the information forever until overwritten or deleting the app. 使用2)可以永久保存信息,直到覆盖或删除该应用程序为止。

using 3) same as the second one but it depends on the app if you really need a database or not. 使用3)与第二个相同,但是是否需要数据库取决于应用程序。

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

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