title: Why on calling first fragment second fragment is automatically getting called?
body: <p>I am using ViewPager to show 4 fragments but the problem is that on loading first fragment second fragment is also getting called i.e. the <code>onActivityCreate()</code> method of both the fragments is getting called together and on swiping to second fragment third fragment's <code>onActivityCreate()</code> and so on for other fragments and for last fragment no <code>onActivityCreate()</code> is getting called. How to resolve that.</p>
  
  <p>Here are my files:</p>
  
  <p>Purchase Details</p>
  
  <pre><code>public class PurchaseDetails extends AppCompatActivity implements ActionBar.TabListener {
  
      ViewPager viewPager;
      ActionBar actionBar;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_purchase_details);
  
  
  
          viewPager=findViewById(R.id.master_purchase_pager);
  
          FragmentManager fm=getSupportFragmentManager();
  
          actionBar = getSupportActionBar();
  actionBar.setTitle("Purchase Details");
          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          viewPager.setAdapter(new PurchaseFragmentAdapter(fm));
          viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
              @Override
              public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
              }
  
              @Override
              public void onPageSelected(int position) {
                  actionBar.setSelectedNavigationItem(position);
              }
  
              @Override
              public void onPageScrollStateChanged(int state) {
  
              }
          });
  
  
  
          ActionBar.Tab tab1 = actionBar.newTab();
          tab1.setText("Date Selector");
          tab1.setTabListener(this);
  
  
          ActionBar.Tab tab2 = actionBar.newTab();
          tab2.setText("Week");
          tab2.setTabListener(this);
  
  
          ActionBar.Tab tab3 = actionBar.newTab();
          tab3.setText("Month");
          tab3.setTabListener(this);
  
  
          ActionBar.Tab tab4 = actionBar.newTab();
          tab4.setText("Year");
          tab4.setTabListener(this);
  
  
  
          actionBar.addTab(tab1);
          actionBar.addTab(tab2);
          actionBar.addTab(tab3);
          actionBar.addTab(tab4);
  
  
      }
  
      @Override
      public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
          viewPager.setCurrentItem(tab.getPosition());
      }
  
      @Override
      public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
  
      }
  
      @Override
      public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
  
      }
  }
  </code></pre>
  
  <p>PurchaseFragmentAdapter</p>
  
  <pre><code>public class PurchaseFragmentAdapter extends FragmentPagerAdapter {
      public PurchaseFragmentAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public Fragment getItem(int position) {
          Fragment fragment=null;
  
          if(position==0)
          {
              fragment= new PurchaseDateSelector();
          }
          else if(position==1)
          {
              fragment= new PurchaseWeek();
          }
          else if(position==2)
          {
              fragment= new PurchaseMonth();
          }
          else if(position==3)
          {
              fragment= new PurchaseYear();
          }
  
  
          return fragment;
      }
  
      @Override
      public int getCount() {
          return 4;
      }
  
  
  }
  </code></pre>
  
  <p>PurchaseDateSelector</p>
  
  <pre><code>public class PurchaseDateSelector extends Fragment
  {
  
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
  
  return inflater.inflate(R.layout.purchase_date_selector_fragment,container,false);
      }
  
  
  
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          Toast.makeText(getActivity(), "Purchase Date Selector", Toast.LENGTH_SHORT).show();
  
  
  }
  
  }
  </code></pre>
  
  <p>PurchaseWeek</p>
  
  <pre><code>public class PurchaseWeek extends Fragment {
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.purchase_week_fragment,container,false);
  
  
      }
  
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          Toast.makeText(getActivity(), "Purchase Week", Toast.LENGTH_SHORT).show();
  
      }
  
  
  }
  </code></pre>
  
  <p>PurchaseMonth</p>
  
  <pre><code>public class PurchaseMonth extends Fragment {
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.purchase_month_fragment,container,false);
      }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          Toast.makeText(getActivity(), "Purchase Month", Toast.LENGTH_SHORT).show();
  
      }
  }
  </code></pre>
  
  <p>PurchaseYear</p>
  
  <pre><code>public class PurchaseYear extends Fragment {
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          return inflater.inflate(R.layout.purchase_year_fragment,container,false);
      }
  
      @Override
      public void onActivityCreated(@Nullable Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
  
          Toast.makeText(getActivity(), "Purchase Year", Toast.LENGTH_SHORT).show();
      }
  }
  </code></pre>