lazy lord
lazy lord

Reputation: 173

how to get last selected action bar tab?

How can I get last selected Action Bar Tab from which I'm navigating to next Tab?.

what I've tried :

actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
                title);

        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(adapter);
        viewPager
                .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

                    /* (non-Javadoc)
                     * @see android.support.v4.view.ViewPager.SimpleOnPageChangeListener#onPageSelected(int)
                     */
                    @Override
                    public void onPageSelected(int position) {
                        // TODO Auto-generated method stub
                        actionBar.setSelectedNavigationItem(position);
                    }

                });

        for (int i = 0; i < adapter.getCount(); i++) {



            ActionBar.Tab tab = actionBar.newTab();

            tab.setText(adapter.getPageTitle(i));
            tab.setTabListener(this);
            actionBar.addTab(tab);

        }

    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        viewPager.setCurrentItem(tab.getPosition());

        // here How Can I get previous selected tab from which I am navigating

        }

So in onTabSelected() method How can I know from which Tab I'm navigated to this Tab.Is there is any way?

Upvotes: 0

Views: 737

Answers (1)

migue02
migue02

Reputation: 130

You can create a field called

int fLastTab = -1;

This field you can update in the function onTabSelected

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    // TODO Auto-generated method stub
    viewPager.setCurrentItem(tab.getPosition());

    // Here you can check the value of fLastTab, 
    // if fLastTab == -1 there wasn't any last selected tab
    // and if it has another value you have the last selected tab
    // at the end of this function you will update the value of fLastTab

    // Doing somenthing with fLastTab....

    fLastTab = tab.getPosition();

}  

There is another way, you can override the function

public void onTabUnselected(Tab tab, FragmentTransaction ft){

    // Doing somenthing with the last selected action bar tab (tab.position())...

}

where you have the position of the tab who exits the selected state. onTabUnselected

Upvotes: 2

Related Questions