Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - onTabChanged not being invoked upon selecting a different tab

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");
}
}   
like image 449
Chip G Avatar asked Sep 26 '11 04:09

Chip G


1 Answers

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?

like image 101
Knickedi Avatar answered Sep 22 '22 15:09

Knickedi