Android中Fragment和Activity之间的数据共享 [英] Data Sharing between Fragments and Activity in Android
问题描述
我之前问过一个类似的问题,但没有得到答案,似乎许多其他人都在寻找答案.所以我发布这个问题是希望得到一个每个人都可以从中受益的明确答案.
Ive asked a similar question before and didn't get an answer and seems many other ppl are searching for an answer. So I am posting this question to hopefully get a clear answer that everyone can benefit from.
我有一个包含 2 个片段的活动.当检查复选框时,我希望Fragment2在活动中设置一个布尔变量,以便Fragment1可以知道复选框是否被选中.
I have an activity with 2 fragments in it. I want fragment2 to set a boolean variable in Activity when a checkbox is checked so that fragment1 can know if the checkbox was checked.
这是我的代码:
活动:
public class modestab extends Activity{
public static Context appContext;
public boolean lf=false;
public void onCreate(Bundle savedInstanceState){
appContext=this;
super.onCreate(savedInstanceState);
ActionBar tabbar= getActionBar();
tabbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab ModesTab =tabbar.newTab().setText("Modes");
ActionBar.Tab CatTab =tabbar.newTab().setText("Categories");
Fragment ModesFragment =new modes();
Fragment CatFragment =new cats();
ModesTab.setTabListener(new MyTabsListener(ModesFragment));
CattTab.setTabListener(new MyTabsListener(CatFragment));
tabbar.addTab(ModesTab);
tabbar.addTab(CatTab);
}
片段 1:(我想读取上面 Acitivity 中设置的布尔值 lf:
Fragment 1:(Where I want to read the boolean lf set in Acitivity above:
@TargetApi(11)
public class tabmodes extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View V=inflater.inflate(R.layout.tab_modes, container, false);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(lf==false) //lf here is the lf in Activity which I want to get
片段 2:我想在 Activity 中设置 lf
Fragment 2: Where I want to set lf in Activity
.....
lifecheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(lifecheck.isChecked())
getActivity().lf=true;//Where I want to set the lf flag in Activity
;
}
});
代码无法编译,我不知道如何在 Activity 中设置 lf 也不知道如何读取它.有人建议我执行 getActivity() 但我看不到变量.
The code doesn't compile and I am not knowing how to set lf in the Activity nor how to read it. Someone suggested I do getActivity() but I am not able to see the variable.
我尝试创建一个函数 setlf(boolean jk) 但我也看不到它...
I tried to create a function setlf(boolean jk) but also I am not able to see it...
欢迎任何帮助:)
推荐答案
多种方式:
1) 活动 -> 片段
- 在您的活动中:创建一个包并使用fragment.setArguments(bundle)
- 在您的 Fragment 中:使用 Bundle bundle = getArguments()
- In your Activity : create a bundle and use fragment.setArguments(bundle)
- in your Fragment : use Bundle bundle = getArguments()
2) 活动 -> 片段
在你的片段中:创建一个公共方法
在您的 Activity 中:调用 active 片段公共方法:
In your Activity : call an active fragment public method :
getSupportFragmentManager().findFragmentById(R.id.your_fragment).publicMethod(args)
getSupportFragmentManager().findFragmentById(R.id.your_fragment).publicMethod(args)
3) 片段 -> 活动
- 在您的 Fragment 中:使用 getter 和 setter 方法创建非接口(回调方法)
- 在你的活动中:实现接口
- In your Fragment : create un interface with getter and setter methods (callback methods)
- In your Activity : implement the interface
4) 片段 -> 活动
- 在您的活动中:创建公共 getter 和 setter 或其他方法
在您的 Fragment 中:使用以下方法调用公共活动 getter、setter 或其他方法:
- In your Activity : Create public getter and setter or other methods
In your Fragment : called public activity getter, setter or other methods using :
getActivity().getSomething(), getActivity().setSomething(args) 或 getActivity().someMethod(args)
getActivity().getSomething(), getActivity().setSomething(args) or getActivity().someMethod(args)
这篇关于Android中Fragment和Activity之间的数据共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!