如何将一些数据转移到另一个片段同样,它是做了额外的意图?
当前回答
如果你使用图表在片段之间导航,你可以这样做:
Bundle bundle = new Bundle();
bundle.putSerializable(KEY, yourObject);
Navigation.findNavController(view).navigate(R.id.fragment, bundle);
片段B:
Bundle bundle = getArguments();
object = (Object) bundle.getSerializable(KEY);
当然你的对象必须实现Serializable
其他回答
为了进一步扩展前面的答案,就像Ankit说的,对于复杂的对象,你需要实现Serializable。例如,对于简单对象:
public class MyClass implements Serializable {
private static final long serialVersionUID = -2163051469151804394L;
private int id;
private String created;
}
在你的FromFragment中:
Bundle args = new Bundle();
args.putSerializable(TAG_MY_CLASS, myClass);
Fragment toFragment = new ToFragment();
toFragment.setArguments(args);
getFragmentManager()
.beginTransaction()
.replace(R.id.body, toFragment, TAG_TO_FRAGMENT)
.addToBackStack(TAG_TO_FRAGMENT).commit();
在你的ToFragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle args = getArguments();
MyClass myClass = (MyClass) args
.getSerializable(TAG_MY_CLASS);
使用Bundle。这里有一个例子:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Bundle为许多数据类型提供了方法。看到这个
然后在你的片段,检索数据(例如在onCreate()方法):
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
如果你使用图表在片段之间导航,你可以这样做:
Bundle bundle = new Bundle();
bundle.putSerializable(KEY, yourObject);
Navigation.findNavController(view).navigate(R.id.fragment, bundle);
片段B:
Bundle bundle = getArguments();
object = (Object) bundle.getSerializable(KEY);
当然你的对象必须实现Serializable
这是你使用bundle的方式:
Bundle b = new Bundle();
b.putInt("id", id);
Fragment frag= new Fragment();
frag.setArguments(b);
从bundle中检索值:
bundle = getArguments();
if (bundle != null) {
id = bundle.getInt("id");
}
来自活动课:
使用bundle参数将数据发送到片段并加载片段
Fragment fragment = new myFragment();
Bundle bundle = new Bundle();
bundle.putString("pName", personName);
bundle.putString("pEmail", personEmail);
bundle.putString("pId", personId);
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
fragment).commit();
从myFragment类:
从bundle中获取参数并将它们设置为xml
Bundle arguments = getArguments();
String personName = arguments.getString("pName");
String personEmail = arguments.getString("pEmail");
String personId = arguments.getString("pId");
nameTV = v.findViewById(R.id.name);
emailTV = v.findViewById(R.id.email);
idTV = v.findViewById(R.id.id);
nameTV.setText("Name: "+ personName);
emailTV.setText("Email: "+ personEmail);
idTV.setText("ID: "+ personId);
推荐文章
- apk (.apk)和应用程序包(.aab)的区别
- 如何设置超时在改造库?
- Android - SPAN_EXCLUSIVE_EXCLUSIVE跨度不能为零长度
- TextView的字体大小在Android应用程序改变字体大小从本机设置
- 如何模拟Android杀死我的进程
- 禁用EditText闪烁光标
- Android Eclipse -无法找到*.apk
- 设置TextView文本从html格式的字符串资源在XML
- 如何允许所有网络连接类型HTTP和HTTPS在Android(9)馅饼?
- Android加载JS包失败
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的