Android - onTabChanged not being invoked upon selecting a different tab
Asked Answered
S

1

5

Thanks for checking out my inquiry!

I have implemented a tab environment as illustrated below. It seems to work as I expected except that it never executes the onTabChanged method. I have found several posts about this type of situation but have not been able to get my code to work the way I expected. Advice?

Thanks, Chip

public class TestTabActivity extends TabActivity implements OnTabChangeListener {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    intent = new Intent().setClass(this, Page1Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    spec = tabHost.newTabSpec("page1").setIndicator("Page 1",
                      res.getDrawable(R.drawable.ic_tab_page1))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Page2Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("page2").setIndicator("Page 2",
                      res.getDrawable(R.drawable.ic_tab_page2))
                  .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Page2Activity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    spec = tabHost.newTabSpec("page3").setIndicator("Page 3",
                      res.getDrawable(R.drawable.ic_tab_page3))
                  .setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}   

public void onTabChanged(String tabId) {
    Log.d("Tab Changed", "Changed a Tab");
}
}   
Spavined answered 26/9, 2011 at 4:23 Comment(0)
C
13

I don't see that you register your class for a tab change with tabHost.setOnTabChangeListener(this).

I would suggest to do that before tabHost.setCurrentTab(0).

Even if it's not called on the first set of the tab you can manually call onTabChanged("page1") at the end of onCreate.

Or did I missunderstood your problem?

Capacitance answered 26/9, 2011 at 4:33 Comment(1)
Thank you Knickedi! I was blind but now I see.Spavined

© 2022 - 2024 — McMap. All rights reserved.