0

I'm using this tutorial AndroidDev, i have pass the value from FragmentA to my FragmentActivity but i have problems with passing from Activity to Fragment. My problem is that I decide to host my fragments in ViewPager. In this particular point I have to get instance of FragmentA or FragmentB form ViewPager. I have call the method onArticleSelected() in my onCreate() method in Activity. I have try this but it return Fragment, not FragmentA or B.

public static class MainActivity extends Activity
    implements HeadlinesFragment.OnHeadlineSelectedListener{
...

public void onArticleSelected(int position) {

    ArticleFragment articleFrag = (ArticleFragment)
            getSupportFragmentManager().findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        articleFrag.updateArticleView(position);
    } else {
        ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.fragment_container, newFragment);
        transaction.addToBackStack(null);

        transaction.commit();
    }
}

}

Community
  • 1
  • 1
Vasil Valchev
  • 5,701
  • 2
  • 34
  • 39
  • Where are FragmentA and FragmentB? where is ViewPager? What exactly is not working? – gpasci Mar 06 '13 at 17:16
  • MainActivity host FragmentA and FragmentB, but it was created from ViewPager, not whit FragmentMenager. I cant get instance in MainActivity of FragmentA or B becouse it will be diffrent from the ViewPager fragments – Vasil Valchev Mar 06 '13 at 17:21
  • 1
    You can assign a tag to your fragments and recall them in activity with findViewByTag(tag) or using the get* methods of ViewPager – gpasci Mar 06 '13 at 17:32
  • TY, i have trying to get current instance from ViewPager, but new instance whit get is more reasonable, if i pass data to fragment it will refresh the fragment – Vasil Valchev Mar 06 '13 at 17:39

1 Answers1

0

android references

Using interface will do the trick to pass data from fragment to activity

Vasil Valchev
  • 5,701
  • 2
  • 34
  • 39