Get index of selected tab in tabHost
Asked Answered
L

3

9

I'm trying to store the index of the currently selected tab in onSaveInstanceState so I can restore it. However the getCurrentTab apparantely gives me back the String I used in the etTabHost().newTabSpec, which I find a bit weird since the documentation says it returns an int and setCurrentTab also taking an int. Does anyone know how I can get the index of my currently selected tab so I can restore it?

Luncheon answered 27/8, 2010 at 10:45 Comment(4)
Are you sure that you invoke the TabHost.getCurrentTab()? It returns int. Probably you misplaced that with getCurrentTabTag()?Hotheaded
I'm certain, that's why I find it so weird.Luncheon
What android version do you use?Hotheaded
I select 2.2 google from the list, but I don't think I'm using any specific features.Luncheon
A
25

you are on the right way, use setOnTabChangedListener to get your selected tab.

public class MainActivity extends TabActivity {
    static TabHost mytabs;

    mytabs = getTabHost();
    mytabs.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
            Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
        }       
    });  
...
...
...
Aluminize answered 27/8, 2010 at 15:31 Comment(1)
I currently don't have access to my laptop with the code so I'll have to try this tomorrow, but why would it make any difference whether I used getCurrentTab in the onTabChanged event or in my onSaveInstanceState? And thank you for your current answer.Luncheon
C
6

You can use getCurrentTab() that returns index of tab start from 0.

Colchicine answered 24/1, 2014 at 11:17 Comment(1)
That method belongs to which object? something.getCurrentTab() and what is something?Dine
B
2

Use tabHost.getCurrentTab() to get Tab ...

tabHost= getTabHost();
tabHost.addTab(tab0); // TabSpec tab0=tabHost.newTabSpec(...
tabHost.addTab(tab1); //  TabSpec tab1=tabHost.newTabSpec

int current = tabHost.getTabHost() ;
Binns answered 11/12, 2014 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.