title: How to get which fragment has been selected
body: <p>I am creating a tab application, using fragments.</p>
  
  <p>I have successfully created tabs, but now I need to create an object when a specific fragment is selected. e.g. When I select <code>SettingsTab</code> I need to call a function that is defined in <code>Test class</code>. and similarly destroy </p>
  
  <pre><code>// SettingsTab
  package com.example.sensorspositioningn;
  
  import android.app.Fragment;
  import android.os.Bundle;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  
  public class SettingsTab extends Fragment {
  
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          // Inflate the layout for this fragment
          return inflater.inflate(R.layout.settingstab, container, false);
  
      }
  }
  </code></pre>
  
  <p>Here is the code in mainActivity:</p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          appContext = getApplicationContext();
  
          //ActionBar
          ActionBar actionbar = getActionBar();
          actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
  
          ActionBar.Tab Tab1= actionbar.newTab().setText("Tab1");
          ActionBar.Tab Tab2= actionbar.newTab().setText("Tab2");
          ActionBar.Tab Tab3= actionbar.newTab().setText("Tab3");
          ActionBar.Tab SettingsTab = actionbar.newTab().setText("Settings");
  
          Fragment Tab1F= new Tab1();
          Fragment Tab2F= new Tab2();
          Fragment Tab3F= new Tab3();
          Fragment mSettingsFragment = new SettingsTab();
  
          Tab1.setTabListener(new MyTabsListener(Tab1F));
          Tab1.setTabListener(new MyTabsListener(Tab2F));
          Tab1.setTabListener(new MyTabsListener(Tab3F));
          SettingsTab.setTabListener(new MyTabsListener(mSettingsFragment));
  
          actionbar.addTab(Tab1);
          actionbar.addTab(Tab2);
          actionbar.addTab(Tab3);
          actionbar.addTab(SettingsTab);
  
  
      }
  protected void onSaveInstanceState(Bundle outState) {
          super.onSaveInstanceState(outState);
          outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
      }
  
  class MyTabsListener implements ActionBar.TabListener {
          public Fragment fragment;
  
          public MyTabsListener(Fragment fragment) {
              this.fragment = fragment;
          }
  
  
          public void onTabReselected(Tab tab, FragmentTransaction ft) {
              Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
          }
  
  
          public void onTabSelected(Tab tab, FragmentTransaction ft) {
              ft.replace(R.id.fragment_container, fragment);
  
  
  
  
          }
  
  
          public void onTabUnselected(Tab tab, FragmentTransaction ft) {
              ft.remove(fragment);
  
  
          }
      }
  </code></pre>