TabHost setCurrentTab only calls the oncreate method for Activity in Tab once

I follow the example here:

http://developer.android.com/resources/tutorials/views/hello-tabwidget.html

Everything works perfectly. The first time I click on each tab, the method oncreatefor the Activity that is bound to that particular tab is called. However, subsequent tab allocations do not call this oncreate method.

I need to be able to execute oncreate (or another method) in an Activity that is bound to each tab when that tab is selected. I know what I can use setOnTabChangedListener, but I'm not sure how to access the Activity bound to the tab so that I can call the oncreate (or other) method.

+5
source share
4 answers

It is a matter of efficiency ... why your method is onCreatenot called twice or more times. The eaiser way to access your activity from yours is TabActivitythrough the OnTabChangedListenerfollowing:

public class YourTabActivity extends TabActivity{
    public void onCreate(Bundle InSavedInstanceState) {
        super.onCreate(InSavedInstanceState);
        final TabHost tabHost = getTabHost();

        // blablabla

        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
            public void onTabChanged(String tabId) {
                if( tabId.equals("the_id_of_your_tab") ){
                    NameOfThatActivity.self.theMethodYouWantToCall();
                }
            }
        });
    }
}

Then, in your child activity, you have something like:

public class NameOfThatActivity extends Activity{

    public static NameOfThatActivity self;

    // blah blah blah
    public onCreate(Bundle b){
        super.onCreate(b);
        self = this;
    }

    public void theMethodYouWantToCall(){
        // do what ever you want here
    }
}

This is not beauty, but it works great.

+4
source

Look at the onStart method in the activity class , I think you want to override this instead of onCreate (or in addition to, usually you only call setContentView on onCreate)

+2
source

, , TabActivity.getCurrentActivity()

+2

@Cristian, . onResume() .

@Override
protected void onResume() {
     super.onResume();               
     // do work 

}
0
source

All Articles