Search code examples
androidlistenertabactivityandroid-tabactivity

TabActivity listener with onCreate()


I have a project with two tabs. Tabs are created in the main class. Here I added the tablistener too, to handle the changes between the tabs. Here is one tab's instant:

TabHost tabHost = getTabHost();    
tabHost.setOnTabChangedListener(this);
    TabHost.TabSpec spec;
    Intent intent;
    intent = new Intent().setClass(this, Tab1.class);
    spec = tabHost.newTabSpec("tab1").setIndicator("",
                    res.getDrawable(R.drawable.ic_tab_tab1))
                    .setContent(intent);
            tabHost.addTab(spec);

The listener method:

public void onTabChanged(String tabName) {
    if (tabName.equals("tab1")){
        tab1.load();
    }
}

And similar for the second tab too. My question is, if the onCreate() methods run only once in the Tab1 and Tab2 classes, how can I "force" the main class to show the corresponding Activity after the tab changes? I receive NullPointerException

The tabs' classes are something like this:

    public class Tab1 extends Activity{
      public void onCreate(Bundle icicle) {
          super.onCreate(icicle);
          setContentView(R.layout.tab1);
          load();
      }
      public void load(){
        //....
      }
    }

Solution

  • onCreate() method calls only once when your Activity first time loaded.

    If you want to perform any functionality on each time you view your Activity, put that functionality in onResume() method.