0

Hi in my project i am using a actionbar with three tabs which are fragments. now i have a button in fragment C, when i click the button i need to swipe back to fragment B, also i want to carry my data from fragment C to B this is secondary, can some one point me in right direction how can i achieve this .

Below is my MainActivty which has all the fragments

public class MainActivity extends FragmentActivity implements
        ActionBar.TabListener {

    private String[] tabs = { "fragA", "fragB", ""fragC", " };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)
        // Initilization
        viewPager = (ViewPager) findViewById(R.id.pager);
        actionBar = getActionBar();
        mAdapter = new TabsPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(mAdapter);

        // Adding Tabs
        for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // on changing the page
                // make respected tab selected
                actionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // on tab selected
        // show respected fragment view
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
    }
teekib
  • 2,821
  • 10
  • 39
  • 75

1 Answers1

0

I think this question is a duplicate, take a look here Android ActionBar Tabs - Swipe code is almost the same: a viewpager, an actionbar and you need transiction between fragments to be done with a swipe gesture.

Hope it helped you out ;)

Community
  • 1
  • 1
Angmar
  • 599
  • 4
  • 9
  • hi i want to navigate on button click, the button is in fragment C – teekib Jan 22 '14 at 12:28
  • 1
    can't you access the actionbar through getSupportActionBar() or something and then setting the element with setSelectedNavigationItem(int index)? I think that that's how it should be done ;) – Angmar Jan 22 '14 at 15:11
  • can u please tell how to get the tab index? – teekib Jan 23 '14 at 05:57
  • you don't need to, look at your tab order starting from zero, if you have tab1,tab1ç2, and tab3 the index of tab1 is 0, the index of tab2 is 1...depending on which order you gave them when you added them to the actionBar – Angmar Jan 23 '14 at 07:44
  • 1
    @Angmar...perfect..thank you so so much. it worked...i did as below actionBar = getActivity().getActionBar(); actionBar.setSelectedNavigationItem(1); – teekib Jan 23 '14 at 10:42