setCurrentTab of a tabHost

2019-02-16 03:27发布

问题:

I'm asking if we can call a setCurrentTab on a tabhost from another class than the class that contains the tabhost and tabspecs?

Can we put

tabHost.setCurrentTab(1);

in another class than this one :

public class Main extends TabActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("", "Welcome in Main");
    setContentView(R.layout.tab);

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);  // Le TabHost a des Tabs

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");  // TabSpec: new tab - TabSpec : setContent to the tab
    firstTabSpec.setIndicator("Informations", getResources().getDrawable(R.drawable.database)).setContent(new Intent(this,FirstTab.class));
    tabHost.addTab(firstTabSpec);

    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    secondTabSpec.setIndicator("Graphiques", getResources().getDrawable(R.drawable.chart)).setContent(new Intent(this,SecondTab.class));
    tabHost.addTab(secondTabSpec);

    TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");  // tid1 is firstTabSpec Id (used to access outside)
    thirdTabSpec.setIndicator("Réglages", getResources().getDrawable(R.drawable.settings)).setContent(new Intent(this,ThirdTab.class));
    tabHost.addTab(thirdTabSpec);
}

}

Can we make it a static var? how Can we achieve this?

Thanks for your attention!

回答1:

You sure can. For your situation, a singleton of the TabActivity may work best. For example:

public class Main extends TabActivity{
    private staic Main theInstance;

    public static getInstance() {
        return Main.theInstance;
    }

    public Main() {
        Main.theInstance = this;
    }

    // The rest of your code here.
}

Then, from another class, you can just call:

Main.getInstance().getTabHost().setCurrentTab(1);

NOTE: What I have provided is not a complete singleton implementation, but it should suffice for what you're doing. Depending on the class you're setting the tab from, you may want to check that Main.getInstance() is not null before calling the setCurrentTab() method.



回答2:

But if you are in another activity, you won't see the tabs anyway will you?

I had a similar problem with a ListView which started the tabHost activity onItemClick to get that working, i passed the tabNumber i wanted to display, through the intent, then used setCurrentTab to set the current tab to the passed tab number. Not sure if this would work for you though.

EDIT: Here is how i did it:

Intent i = new Intent(this, TabHostActivity.class);
i.putExtra("TabNumber", tabNumberToBeSelected); //tabNumberToBeSelected is an int
startActivity(i);

and then in the tabHost activity:

int tabNumber = getIntent().getIntExtra("TabNumber", 0);

//tabHost code goes here

tabHost.setCurrentTab(tabNumber);    


回答3:

In your activities FirstTab, SecondTab and ThirdTab you should use the method like:

boolean setCurrentTab(int i) {
    if (getParent() instanceof Main) {
        ((Main) getParent()).getTabHost().setCurrentTab(i);
        return true;
    }
    return false;
}