Android Tabhost refresh

12,300

Solution 1

I think this is what you want

 tabHost.addTab(tabHost.newTabSpec("tab1")
        .setIndicator("First Text")
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        .setContent(new Intent(this, class1.class)));
 tabHost.addTab(tabHost.newTabSpec("tab2")
        .setIndicator("Second Text")
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        .setContent(new Intent(this, class2.class)));

Just use .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) to your tab class and get your desired result

Solution 2

Just use onResume function. For me works just fine.

Solution 3

Use http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html

For Example :

  mTabHost.setOnTabChangedListener(new OnTabChangeListener(){
  @Override
 public void onTabChanged(String tabId) {
    if(TAB_1.equals(tabId)) {
     //Do first activity task
  }
  if(TAB_2.equals(tabId)) {
      //Do the other task...so on
  }
}});

Edited :

If you want to see that a particular tab is clicked, you need to add your listener to the tab itself, not the TabHost.

The hierarchy of views in a tab implementation is:

TabHost
    TabWidget
        (tab)
        (tab)
    FrameLayout

The tabs are added at runtime by calling: tabHost.addTab(tabHost.newTabSpec(""));

You can then get a handle to the individual tabs by calling: getTabWidget().getChildAt(4);

In essence, you are adding your OnClickListener to a child of the TabWidget. You can now pick up the clicks on your individual tab. However, this will override the default behavior which changes the content when a tab is clicked. So, to get your content to change, your OnClickListener will need to do that for you.

Here is a full example, which lets you intercept the click event, and change the content below the tab:

 final String myTabTag = "My Tab";
 final int myTabIndex = 3;

 getTabHost().addTab( getTabHost().newTabSpec(myTabTag) );

 getTabWidget().getChildAt(myTabIndex).setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
     if (getTabHost().getCurrentTabTag().equals(myTabTag)) {
        getTabHost().setCurrentTab(myTabIndex );
    }
}
});
Share:
12,300
thathashd
Author by

thathashd

Updated on June 17, 2022

Comments

  • thathashd
    thathashd about 2 years

    I have the following code to tabhost.

    tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("First Text")
                .setContent(new Intent(this, class1.class)));
    tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("Second Text")
                .setContent(new Intent(this, class2.class)));
    

    How can i refresh each tab? I want to do this because i have some problems showing data from database.

    tanks