1

I have an ActionBarActivity which implements ActionBar.TabListener as shown below

public class MainActivity extends ActionBarActivity implements
        ActionBar.TabListener {
viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mAdapter);

    Tab tab1 = actionBar.newTab();
    tab1.setText("Home");
    tab1.setTabListener(this);
    actionBar.addTab(tab1);

//I have other few tabs as well }

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            // TODO Auto-generated method stub
            actionBar.setSelectedNavigationItem(arg0);
        }

I have some ListFragment like the one below..

public class Home extends ListFragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.home, container, false);
    return rootView;
}
}

I need to send certain key or value from MainActivity to ListFragment according to certain condition. How do I achieve that? And how do I recieve those values from ListFragment? And I am using ViewPager as well. So I won't be able to pass static fragment name.

Nabin
  • 11,216
  • 8
  • 63
  • 98

1 Answers1

1

You can do like this -

MainActivity class -

public class MainActivity extends ActionBarActivity implements
    ActionBar.TabListener {

    private String myString = "hello";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ...
    }

    public String getMyData() {
        return myString;
    }
}

Home Fragment class -

public class Home extends ListFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, 
    ViewGroup container, Bundle savedInstanceState) {
         View rootView = inflater.inflate(R.layout.home, container, false);

        MainActivity activity = (MainActivity) getActivity();
        String myDataFromActivity = activity.getMyData();
        return rootView;
    }
}
sjain
  • 23,126
  • 28
  • 107
  • 185