我正在使用这个教程AndroidDev,我已经将FragmentA中的值传递给了FragmentActivity,但是从Activity转移到Fragment时遇到了问题。我的问题是我决定在ViewPager中托管我的片段。在这一特定点上,我必须从ViewPager获取FragmentA或FragmentB的实例。我在Activity中的onCreate()方法中调用了onArticleSelected()方法。我试过this但是它返回Fragment,而不是FragmentA或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();
}
}
}