使用Bundle
。这是一个例子:
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
Bundle为许多数据类型放置了方法。看到这个
然后在您的中Fragment
,使用以下方法检索数据(例如,在onCreate()
方法中):
Bundle bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt(key, defaultValue);
}
嗨,thanx为您解答,但是我们需要实现任何东西吗?例如Serializable或Parcelable?
不,您不需要实现任何类。
可能想添加检查以查看该捆绑包!=空,然后再尝试从中取出任何东西?
如果内存中已有片段?
这是代码不起作用,无法将活动重定向到包含数据的片段