4

FragmentPagerSupport is a FragmentActivity class, FragmentA and FragmentB are representing 2 different tabs. In the First tab I have a EditText and a button. My task is to on button click need to open the 2nd tab and show the EditText value in the 2nd tab. I am also using a FragmentStatePagerAdapter.

Following code is building tabs onCreate() of FragmentActivity:

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

My code in FragmentStatePagerAdapter is as follow:

public Fragment getItem(int position) {
    Fragment fragment = null;
    Bundle args = new Bundle();         
    switch (position) {
    case 0:
        fragment = FragmentA();
        args.putInt(FragmentA.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
    break;
    case 1:
        fragment = new FragmentB();
        args.putInt(FragmentB.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
    break;
    }
return fragment;
}

My code of FragmentA on button click is as follow:

confirmButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {


    }
}

Now I stuck here. Can't find what should be the code here to call the 2nd tab and show the value according to tab1 EditText value.

ray
  • 4,210
  • 8
  • 35
  • 46

1 Answers1

12

Assuming your ViewPager is named mPager:

public void onClick(View view)
{
     mPager.setCurrentItem(1); // Change to page 1, i.e., FragmentB
}

I would suggest reading this training page in detail. It goes into some of the strategies used to pass information from Fragment to Fragment (generally by having the Activity implement an interface that the Fragment can then call).

It really depends on how coupled you want your Fragments to be. You could certainly use something like

((FragmentB)getActivity().getSupportFragmentManager().findFragmentByTag(
    "android:switcher:" + pager.getId() + ":1")).setExitText(text)

to pass the text from FragmentA to FragmentB, but that tightly couples your Fragments to each other and to the ViewPager the Activity contains.

I would suggest

  1. Make your Activity implement an Interface that contains a single method such as goToFragmentB(String exitText)
  2. Call this method in your FragmentA's onClick
  3. Have goToFragmentB:
    • Call a setExitText in FragmentB (use the above findFragmentByTag using that odd tag per the ViewPager source)
    • Set the pager to FragmentB
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks but How I will get the ViewPager in FragmentA? – ray Mar 01 '13 at 05:43
  • 2
    getActivity().findViewById(R.id.pager) should work (using whatever your ID is for the ViewPager). – ianhanniballake Mar 01 '13 at 05:51
  • Thanks a lot bro. It really works. Now please give a solution for my another question - How I will pass the value of 1st tab ExitText (input by user) to 2nd tab? – ray Mar 01 '13 at 05:58
  • See my edited solution and make sure you read the linked training page on communicating between fragments closely. – ianhanniballake Mar 01 '13 at 06:45