开发者

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

开发者 https://www.devze.com 2023-03-23 08:13 出处:网络
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

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);
    tab开发者_JS百科Host.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?


Try to use

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

Where Object = any display Object.


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.
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号