gpt4 book ai didi

android - 如何将参数传递给 fragment ?

转载 作者:行者123 更新时间:2023-11-29 01:34:43 31 4
gpt4 key购买 nike

我正在尝试将对象传递到我的 Fragment。我是这样做的:

MenuFragment fragment = new MenuFragment(item);
setFragment(fragment);

我知道为 Fragment 使用带参数的构造函数是不对的。所以我这样做了:

public static MenuFragment newInstance(String desc) {
MenuFragment f = new MenuFragment();
Bundle b = new Bundle();
b.putString("desc", desc);
f.setArguments(b);
return f;
}

但我不知道如何传递字符串和我的自定义对象?

最佳答案

编辑:如果你想传递一些自定义对象使用

bundle.putSerializable(key, object)

方法。您的对象必须实现 java.io.Serializable 接口(interface)。

然后在您的 fragment 中调用 getArguments() 方法:

public View onCreateView(LayoutInflater inflater,ViewGroup containerObject, Bundle savedInstanceState){
//...

Bundle bundle = getArguments();
Myclass itemObject = (Myclass) bundle.getSerializable("yourKey");

//...
}

关于android - 如何将参数传递给 fragment ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29072030/

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