如何将参数从 FragmentActivity 传递给 Fragment

How to pass parameter to Fragment from FragmentActivity

我的 android 应用程序中有一个 FragmentActivity。 我使用 viewPager 在 activity 中插入标签。所以我有这段代码。有效。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_clienti);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String datas= extras.getString("Cliente");
            if (datas!= null) {
                System.out.println("pippo");
            } 
        }

        TabAdapter = new DemoCollectionPagerAdapter(getSupportFragmentManager());
        Tab = (ViewPager)findViewById(R.id.pager);
        Tab.setOnPageChangeListener(
                new ViewPager.SimpleOnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        actionBar = getActionBar();
                        actionBar.setSelectedNavigationItem(position);                    }
                });
        Tab.setAdapter(TabAdapter);
        actionBar = getActionBar();
        //Enable Tabs on Action Bar
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        ActionBar.TabListener tabListener = new ActionBar.TabListener(){
            @Override
            public void onTabReselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
            @Override
            public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
                Tab.setCurrentItem(tab.getPosition());
            }
            @Override
            public void onTabUnselected(android.app.ActionBar.Tab tab,
                    FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }};
            //Add New Tab
            actionBar.addTab(actionBar.newTab().setText("Scheda Cliente").setTabListener(tabListener));
            actionBar.addTab(actionBar.newTab().setText("Conto Cliente").setTabListener(tabListener));

            apriConnessioneDB();
    }

这是 class FragmentStatePagerAdapter

public class DemoCollectionPagerAdapter extends FragmentStatePagerAdapter {
    public DemoCollectionPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int i) {
        switch (i) {
        case 0:
            //Fragement for Android Tab
            return new infoCliente();
        case 1:
           //Fragment for Ios Tab
            return new infoCliente();
        case 2:
            //Fragment for Windows Tab
            return new infoCliente();
        }
    return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }
}

这是 class 片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.view_infocliente, container, false);
        if(savedInstanceState!=null){
            //String tmp = savedInstanceState.getString("myKey");

            //TextView  mTabHost = (TextView) findViewById(android.R.id.labelRagioneSociale);
            System.out.println("PIPPPPPPPPPPPPPPPPPPPPPP");
        }
        TextView btn=(TextView) view.findViewById(R.id.labelRagioneSociale5);  
        btn.setText("pippo");
        return view;
    } 

现在我想知道如何将参数传递给 infoCliente class。 在第一个 class 的 onCreate 方法中,我有这个:

String datas= extras.getString("Cliente");

我想将此参数传递给片段 class。

在你的Fragment中定义一个接口,例如

public interface FragmentCallback {

    onSomethingHappend(String param);

}

在您的 Activity 中实现接口。

然后,当你想调用回调时,做这样的事情:

FragmentCallback callback = (FragmentCallback) getActivity();
callback.onSomethingHappened(string);
Bundle bundle = new Bundle();
bundle.putInt("battery", bat);
MyFragment fragment=new MyFragment();
fragment.setArguments(bundle);

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment_container,fragment);
ft.commit();

然后在片段的onCreate()方法中:

 Bundle bundle=getArguments();  int mLabel = bundle.getInt("battery",0);