我的代码包含一个主 Activity 和其中的三个 fragment ,我想将一个值从容器 Activity 传递到其 fragment ,但它不起作用。
我试图制作一个接口(interface)来相互通信,但什么也没发生。
我也尝试制作 bundle ,但在 setArguments
中出现错误。
Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);
并在 fragment 中
Bundle bundle = this.getArguments();
if(bundle != null){
int i = bundle.getInt(key, defaulValue);
}
你能帮我一下吗? 谢谢!
最佳答案
尝试在您的 Activity 中使用 Intent :
Intent a = new Intent (this, yourfragment.class);
a.putInt(key, value);
setIntent(a);
在你的 fragment 中:
ActivityName activity = (ActivityName) getActivity();
Intent b= activity.getIntent();
int Uid = b.getIntExtra(key);
关于Android:将值从容器 Activity 传递到其 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19894681/