如何与他的父母从Fragmet变成Fragment?

时间:2018-12-04 01:34:50

标签: android android-fragments android-intent

Fragment1与Fragment2有一个不同的父级,实际上我想与他的父级一起访问Fragment2。

Fragment2 fragment = new Fragment2().getParentFragment();
fragment.setArguments(bundle);
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.containerView,fragment);
ft.commit();

1 个答案:

答案 0 :(得分:0)

您必须按意图将数据传递给secondActivity,然后使用setData方法将数据传递给片段。注意,您必须在片段的onCreateView函数中执行此操作。像下面的代码:

public void setData(Data data){
this.data = data;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
  View rootView = inflater.inflate(R.layout.fragment_layout,container,false);


  // use data here
  // also use view components(like buttons) here 


  return rootView;
}