gpt4 book ai didi

android - 如何在 Fragments 中使用 setArguments() 和 getArguments() 方法?

转载 作者:IT老高 更新时间:2023-10-28 13:06:13 28 4
gpt4 key购买 nike

我有 2 个 fragment :(1)Frag1 (2)Frag2。

fragment 1

bundl = new Bundle();
bundl.putStringArrayList("elist", eList);

Frag2 dv = new Frag2();
dv.setArguments(bundl);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.the_fragg,dv);
ft.show(getFragmentManager().findFragmentById(R.id.the_fragg));
ft.addToBackStack(null);
ft.commit();

如何在 Frag2 中获取这些数据?

最佳答案

只需在 Frag2onCreateView() 方法中调用 getArguments():

public class Frag2 extends Fragment {

public View onCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your arguments
Bundle bundle=getArguments();

//here is your list array
String[] myStrings=bundle.getStringArray("elist");
}
}

编辑:

最佳实践是在 onCreate 方法中读取和保存参数。在 onCreateView 中这样做更糟糕,因为每次 fragment 创建 View 时都会调用 onCreateView(例如每次 fragment 从后台弹出时)

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle arguments = getArguments();
}

关于android - 如何在 Fragments 中使用 setArguments() 和 getArguments() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5425568/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com