0

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(){
        //....
      }
    }
MMMM
  • 1,319
  • 1
  • 14
  • 32
  • if you're using Lists in both Tabs and want them to be re-populated each time you click then you will have to use this on onTabChanged() method. also I have implemented createTabContent(String s) of the TabHost.TabContentFactory interface to return different views(listviews in my case) depending on the tab on load of the app. – Sergey Benner Jan 27 '12 at 10:08
  • Where do you get null pointer exception? please specify the line. – Hiral Vadodaria Jan 27 '12 at 10:17
  • It's ok now. The second tab wasn't added well. The onResume() method was the solution for my problem. Thanks you all:) – MMMM Jan 31 '12 at 13:42

1 Answers1

4

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.

Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
MrWaqasAhmed
  • 1,479
  • 12
  • 12