安卓:从容器中的活动的值传递给它的片段 [英] Android: pass a value from container Activity to its fragment

查看:97
本文介绍了安卓:从容器中的活动的值传递给它的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的code包含一个主要活动和里面三个片段,我想通过从容器活动的片段的价值,但它不工作。
我试图让一个接口,传达给对方,但什么都没有发生。
我也试着做一个包,但是我在 setArguments 错误。

My code contains a main Activity and three fragments inside it and I want to pass a value from the container activity to its fragment, but it's not working.
I tried to make an interface to communicate to each other but nothing happened.
I also tried to make a bundle but I have an error in setArguments.

Bundle bundle = new Bundle();
bundle.putInt(key, value);
fragment.setArguments(bundle);

和在片段

Bundle bundle = this.getArguments();
if(bundle != null){
    int i = bundle.getInt(key, defaulValue);
}

你能帮帮我吗? 谢谢!

Can you please help me? Thank you!

推荐答案

试着在你的活动的意图:

Try with an Intent in your activity :

Intent a = new Intent (this, yourfragment.class);
a.putInt(key, value);
setIntent(a);

而在你的片段:

And in your fragment :

ActivityName activity = (ActivityName) getActivity();
Intent b= activity.getIntent();    
int Uid = b.getIntExtra(key);

这篇关于安卓:从容器中的活动的值传递给它的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆