title: Tabs execute the codes present in their next fragments in android
body: <p>I am trying to implement <code>Tabs</code> with swipe using <code>ViewPager</code> and 4 <code>Fragments</code>. When I swipe the tabs the respective <code>xml layouts</code> of the fragmnets are displayed correctly on each tab but the code of the fragments are not executed correctly. 
  eg-The following tabs have the respective fragments in it.</p>
  
  <p><strong>Tab0-ButtonFragment</strong></p>
  
  <pre><code>public class ButtonFragment extends Fragment {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.fragment_button, container, false);
      Log.i("inside", "button fragment");
      return rootView;
  }
  </code></pre>
  
  <p><strong>Tab1-ImageFragment</strong></p>
  
  <pre><code>public class ImageFragment extends Fragment {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.fragment_image, container, false);
      Log.i("inside", "image fragment");
      return rootView;
  }
  </code></pre>
  
  <p><strong>Tab2-TextFragment</strong></p>
  
  <pre><code>    public class TextFragment extends Fragment {
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.fragment_text, container, false);
      Log.i("inside", "text fragment");
      return rootView;
  }
  </code></pre>
  
  <p><strong>Tab3-Test</strong>
      public class Test extends Fragment {</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
  
      View rootView = inflater.inflate(R.layout.temp, container, false);
      Log.i("inside", "tessssssssst fragment");
      return rootView;
  }
  </code></pre>
  
  <p>When the tabs are displayed 
  I get the log message for tab0 and tab1 simultaneously. Then after swiping and going to tab1 i get the log message for tab2. After swiping to tab2 i get log message for tab 3 and when tab 3 is reached no log message is dispalyed. Can anyone tell me how can I execute the respective codes for the respective tabs? My remaining codes are as below:</p>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter { //Update - code formatting
  
  public TabsPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem( int index) {
      Log.i("index", "" + index);
      switch (index) {
      case 0:
          // Top Rated fragment activity
          return new ButtonFragment();
      case 1:
          // Games fragment activity
          return new ImageFragment();
      case 2:
          // Movies fragment activity
          return new TextFragment();
      case 3:
          return new Test();
      }
  
      return null;
  }
  
  @Override
  public int getCount() {
      // get item count - equal to number of tabs
      return 4;
  }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>       public class MainActivity extends FragmentActivity implements OnTabChangeListener, OnPageChangeListener {
  
  private TabsPagerAdapter mAdapter;
  private ViewPager mViewPager;
  private TabHost mTabHost;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      mViewPager = (ViewPager) findViewById(R.id.viewpager);
  
      // Tab Initialization
      initialiseTabHost();
      mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
      // Fragments and ViewPager Initialization
  
  
      mViewPager.setAdapter(mAdapter);
      mViewPager.setOnPageChangeListener(MainActivity.this);
  }
  
  // Method to add a TabHost
  private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec) {
      tabSpec.setContent(new MyTabFactory(activity));
      tabHost.addTab(tabSpec);
  }
  
  // Manages the Tab changes, synchronizing it with Pages
  public void onTabChanged(String tag) {
      int pos = this.mTabHost.getCurrentTab();
      this.mViewPager.setCurrentItem(pos);
  }
  
  @Override
  public void onPageScrollStateChanged(int arg0) {
  }
  
  // Manages the Page changes, synchronizing it with Tabs
  @Override
  public void onPageScrolled(int arg0, float arg1, int arg2) {
      int pos = this.mViewPager.getCurrentItem();
      this.mTabHost.setCurrentTab(pos);
  }
  
  @Override
      public void onPageSelected(int arg0) {
  
  }
  
  
  // Tabs Creation
  private void initialiseTabHost() {
      mTabHost = (TabHost) findViewById(android.R.id.tabhost);
      mTabHost.setup();
  
      // TODO Put here your Tabs
      MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("ButtonTab").setIndicator("ButtonTab"));
      MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("ImageTab").setIndicator("ImageTab"));
      MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("TextTab").setIndicator("TextTab"));
      MainActivity.AddTab(this, this.mTabHost, this.mTabHost.newTabSpec("TestingTab").setIndicator("TestingTab"));
  
      mTabHost.setOnTabChangedListener(this);
  }
  </code></pre>