片段和活性的Andr​​oid之间的数据共享 [英] Data Sharing between Fragments and Activity in Android

查看:125
本文介绍了片段和活性的Andr​​oid之间的数据共享的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院之前问过类似的问题,并没有得到答案,似乎许多其他的脂肪酶在寻找一个答案。所以我张贴了这个问题,希望能够得到一个明确的答案,每个人都可以从中受益。

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设置在活动一个布尔变量,当复选框被选中,这样片段1可以知道,如果该复选框被检查。

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.

这是我的code:

活动:

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:(这里我想读的布尔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:如果我想设置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
                  ;
              }
          });

在code不能编译,我不知道如何设置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(布尔JK),而且我不能够看到它...

I tried to create a function setlf(boolean jk) but also I am not able to see it...

任何帮助是值得欢迎的:)

Any help is welcome :)

推荐答案

方法很多:

一)活动 - >片段

  • 在活动的:创建一个包,并使用片段<一href="http://developer.android.com/reference/android/app/Fragment.html#setArguments%28android.os.Bundle%29">setArguments(bundle)
  • 在片段的:使用束束= <一个href="http://developer.android.com/reference/android/app/Fragment.html#getArguments%28%29">getArguments()
  • In your activity : create a bundle and use fragment.setArguments(bundle)
  • in your fragment : use Bundle bundle = getArguments()

2)的片段 - >活动

  • 在片段的:创建联合国接口getter和setter方法​​(<一href="http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity">callback方法的)
  • 在活动的:实现接口
  • In your fragment : create un interface with getter and setter methods (callback methods)
  • In your activity : implement the interface

3)片段 - >活动

  • 在活动的:建立公共的getter和setter或其他方法
  • 在片段的:所谓的公共活动的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 :

<一个href="http://developer.android.com/reference/android/support/v4/app/Fragment.html#getActivity%28%29">getActivity().getSomething(), <一href="http://developer.android.com/reference/android/support/v4/app/Fragment.html#getActivity%28%29">getActivity().setSomething(args)或<一href="http://developer.android.com/reference/android/support/v4/app/Fragment.html#getActivity%28%29">getActivity().someMethod(args)

getActivity().getSomething(), getActivity().setSomething(args) or getActivity().someMethod(args)

4)活动 - >片段

  • 在片段的:创建一个公共方法

  • In your fragment : create a public method

在活动的:调用有效片段public方法:

In your activity : call an active fragment public method :

<一个href="http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#getSupportFragmentManager%28%29">getSupportFragmentManager().findFragmentById(R.id.your_fragment).publicMethod(args)

getSupportFragmentManager().findFragmentById(R.id.your_fragment).publicMethod(args)

这篇关于片段和活性的Andr​​oid之间的数据共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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