title: How to stop tab change from running fragment onCreateView in Andriod
body: <p>I have three tabs using the implementation below and they perform very well. When tab is changed the proper fragment is load and so on. The problem is that, when i get to the last tab and comeback to the first fragment, its like its oncreateview method is always triggered again running the other codes it in causing duplicates. Any help will be greatly appreciated.</p>
  
  <p>//Activity on the tab is based</p>
  
  <pre><code>public class Dashboard extends AppCompatActivity {
  
  private TabLayout tabLayout;
  private ViewPager viewPager;
  private MyViewPagerAdapter myViewPagerAdapter;
  private int[] tabIcon = {R.drawable.ic_home, R.drawable.ic_message, R.drawable.ic_person};
  android.support.v7.widget.Toolbar toolbar;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.dashboard);
      //Toolbar
      toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);
  
      //ViewPager
      viewPager = (ViewPager) findViewById(R.id.viewpager);
      setupViewPager(viewPager);
  
      //Tablayout
      tabLayout = (TabLayout) findViewById(R.id.tabs);
      tabLayout.setupWithViewPager(viewPager);
      tabLayout.getTabAt(0).setIcon(tabIcon[0]);
      tabLayout.getTabAt(1).setIcon(tabIcon[1]);
      tabLayout.getTabAt(2).setIcon(tabIcon[2]);
  
      viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
          @Override
          public void onTabSelected(TabLayout.Tab tab) {
              switch(tab.getPosition()) {
                  case 0:
                      viewPager.setCurrentItem(0);
                      toolbar.setTitle("Home");
                      break;
                  case 1:
                      viewPager.setCurrentItem(1);
                      toolbar.setTitle("Messages");
                      break;
                  case 2:
                      viewPager.setCurrentItem(2);
                      toolbar.setTitle("Profile");
                      break;
              }
          }
  
          @Override
          public void onTabUnselected(TabLayout.Tab tab) {
  
          }
  
          @Override
          public void onTabReselected(TabLayout.Tab tab) {
  
          }
  
  
      });
  }
  
  private void setupViewPager(ViewPager viewPager){
      myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager());
      myViewPagerAdapter.addFragments(new CategoryFragment(), "Categories");
      myViewPagerAdapter.addFragments(new MessagesFragment(), "Messages");
      myViewPagerAdapter.addFragments(new ProfileFragment(), "Profile");
      viewPager.setAdapter(myViewPagerAdapter);
  }
  
  //View Pager Adapter
  public class MyViewPagerAdapter extends FragmentPagerAdapter {
  
      ArrayList&lt;Fragment&gt; fragments = new ArrayList&lt;&gt;();
      ArrayList&lt;String&gt; tabTitles = new ArrayList&lt;&gt;();
  
      public void addFragments(Fragment fragments, String titles){
  
          this.fragments.add(fragments);
          this.tabTitles.add(titles);
      }
  
      public MyViewPagerAdapter(FragmentManager fm) {
          super(fm);
      }
  
      @Override
      public int getCount() {
          return fragments.size();
      }
  
      @Override
      public Fragment getItem(int position) {
          return fragments.get(position);
      }
  
      @Override
      public CharSequence getPageTitle(int position) {
          //return tabTitles.get(position);
          return null;
      }
  }
  
  
  
  @Override
  public void onBackPressed() {
      super.onBackPressed();
  }
  </code></pre>
  
  <p>}</p>
  
  <p>//Main first fragment code
  public class CategoryFragment extends Fragment {</p>
  
  <pre><code>private DBHandler dbHandler;
  private ListView listView;
  private ListAdapter adapter;
  ArrayList&lt;Categories&gt; categoriesList = new ArrayList&lt;Categories&gt;();
  
  public CategoryFragment() {
      // Required empty public constructor
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view =  inflater.inflate(R.layout.fragment_home, container, false);
  
      //Setting up the basic categories
      dbHandler = new DBHandler(view.getContext());
  
      //Get Categories from database
      final Cursor cursor = dbHandler.getCategories(0);
      if (cursor != null) {
          if(cursor.moveToFirst()){
              do{
                  Categories categories = new Categories();
                  categories.set_id(cursor.getInt(0));
                  categories.set_categoryname(cursor.getString(2));
                  categories.set_categoriescaption(cursor.getString(3));
                  categoriesList.add(categories);
  
              }while (cursor.moveToNext());
          }
          cursor.close();
      }
  
      listView = (ListView) view.findViewById(R.id.categories);
  
      adapter = new CategoryAdapter(view.getContext(), R.layout.cursor_row, categoriesList);
      listView.setAdapter(adapter);
  
      listView.setOnItemClickListener(
              new AdapterView.OnItemClickListener() {
                  @Override
                  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  
                      Integer cid = (int) (long) adapter.getItemId(position);
  
                      TextView categoryname = (TextView) view.findViewById(R.id.cursor);
                      String cname = categoryname.getText().toString();
  
                      Intent i = new Intent(view.getContext(), CategoryList.class);
                      i.putExtra("categoryname", cname);
                      i.putExtra("categoryid", cid);
                      startActivity(i);
                  }
              }
      );
  
      return view;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>So when i swipe back here from the last tab. OncreateView runs again. How can i handle that and prevent duplicates. Thank you</p>