title: Android: Detect single fragment viewpager with Tab
body: <p>I have a problem with android a <code>ViewPager</code> setup with <code>FragmentPagerAdapter</code> and <code>TabLayout</code> </p>
  
  <p><code>TabLayout</code> works fine. But, the code in all tabs run simultaneously. I want the code in the currently selected tab to run</p>
  
  <p>This is my activity</p>
  
  <pre><code>package francesco.prisco.siamostudenti.ui;
  
  import android.os.Bundle;
  import android.support.design.widget.TabLayout;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentPagerAdapter;
  import android.support.v4.app.FragmentTransaction;
  import android.support.v4.view.ViewPager;
  import android.support.v7.app.AppCompatActivity;
  import android.support.v7.widget.Toolbar;
  import android.util.Log;
  import java.util.ArrayList;
  import java.util.List;
  import francesco.prisco.siamostudenti.R;
  import francesco.prisco.siamostudenti.ui.fragments.CalendarFragment;
  import francesco.prisco.siamostudenti.ui.fragments.ChatFragment;
  import francesco.prisco.siamostudenti.ui.fragments.EmailFragment;
  import francesco.prisco.siamostudenti.ui.fragments.HomeFragment;
  import francesco.prisco.siamostudenti.ui.fragments.ProfiloFragment;
  
  
  public class HomeTabActivity extends AppCompatActivity {
  
  //  private Toolbar toolbar;
      private TabLayout tabLayout;
      private ViewPager viewPager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home_tab);
  
         //     toolbar = (Toolbar) findViewById(R.id.toolbar);
         //    setSupportActionBar(toolbar);
         //    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  
          viewPager = (ViewPager) findViewById(R.id.viewpager);
          setupViewPager(viewPager);
  
          tabLayout = (TabLayout) findViewById(R.id.tabs);
          tabLayout.setupWithViewPager(viewPager);
          setupTabIcons();
      }
      private void setupTabIcons() {
          int[] tabIcons = {
              R.drawable.ic_home,
              R.drawable.ic_calendar,
              R.drawable.ic_chat,
              R.drawable.ic_email,
              R.drawable.ic_profile
          };
  
          tabLayout.getTabAt(0).setIcon(tabIcons[0]);
          tabLayout.getTabAt(1).setIcon(tabIcons[1]);
          tabLayout.getTabAt(2).setIcon(tabIcons[2]);
          tabLayout.getTabAt(3).setIcon(tabIcons[3]);
          tabLayout.getTabAt(4).setIcon(tabIcons[4]);
      }
  
      private void setupViewPager(ViewPager viewPager) {
          ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
          adapter.addFrag(new HomeFragment(), "HOME");
          adapter.addFrag(new CalendarFragment(), "CALENDAR");
          adapter.addFrag(new ChatFragment(), "CHAT");
          adapter.addFrag(new EmailFragment(), "EMAIL");
          adapter.addFrag(new ProfiloFragment(), "PROFILO");
          viewPager.setAdapter(adapter);
      }
      class ViewPagerAdapter extends FragmentPagerAdapter {
          private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;();
          private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
  
          public ViewPagerAdapter(FragmentManager manager) {
              super(manager);
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              return mFragmentList.size();
          }
  
          public void addFrag(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
  
              // return null to display only the icon
              return null;
           }
       }
   }
  </code></pre>
  
  <p>And this is code of a fragment</p>
  
  <pre><code>package francesco.prisco.siamostudenti.ui.fragments;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.Toast;
  
  import francesco.prisco.siamostudenti.R;
  import francesco.prisco.siamostudenti.ui.HomeTabActivity;
  
  public class ProfiloFragment extends Fragment {
      public ProfiloFragment() {}
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_profilo, container, false);
          return rootView;
      }
  
  }    
  </code></pre>
  
  <p>Help me please</p>