Search code examples
androidmethodstabsswitch-statementandroid-tabhost

Android: Tab switch - How to update display when switching between tabs?


The app I am building has a tabhost with three tabs. The goal is to update the display in the new tab when the switch takes place. I have the following tab change listener set up in the main activity that built the tab host.

tabHost.setOnTabChangedListener(new OnTabChangeListener(){
  public void onTabChanged(String tabId) {
    //Now what?
  }
});

The question is, How do I take the tabId given and use it to call a method in that tab?

Edit

To clarify a bit: when you create an Intent for a new tab you specify an Activity Class, an object of which, presumably, is created to handle setup and management of that tab.

    intent = new Intent().setClass(this, Setup.class);
    spec = tabHost.newTabSpec("setup").setIndicator("",
      res.getDrawable(R.drawable.tab_setup))
        .setContent(intent);
    tabHost.addTab(spec);

What I am looking for with this question is how to get a reference to that object? In the example above, the Setup class is instantiated to handle the “Setup Tab”.

To restate my question now: How do I, from the OnTabChangeListener, call a method in the Setup class?


Solution

  • As it turns out the solution to the problem is closer to home. Rather than attaching code to the TabHost, the solution involves getting into the Life-Cycle of the activity by overriding the onPause and onResume methods.

    @Override
    public void onResume() {
        super.onResume();
        // Update your UI here.
    }