将数据从活动发送到片段

时间:2012-06-13 05:19:50

标签: android android-fragments

我们如何将数据从活动发送到片段?使用FragmentPagerAdapter将片段配置为活动。

此致 迷你。

2 个答案:

答案 0 :(得分:1)

  • 您可以使用setArguments在创建时将包传递给片段。
  • 您可以创建在Fragment类上设置数据的方法。

答案 1 :(得分:0)

您可以使用Bundle

执行此操作

从活动(或片段)发送数据:

int a = 5;

Bundle args = new Bundle(); 
args.putInt("INT_DATA_TAG", a); 

Fragment fragment = Fragment.newInstance(args); 
//Making fragment transaction 

检索片段中的数据

int a;
public static Fragment newInstance(Bundle args) {
      a = args.getInt("INT_DATA_TAG"); //use a constant for the tag
      return new Fragment();
}