title: How to get current selected Fragment in FragmentTabHost
body: <p>There are some questions related to this same issue. For example, <a href="https://stackoverflow.com/questions/16616495/findfragmentbytag-looking-for-fragment-in-fragmenttabhost-always-null">this one</a>. But it doesn't work.</p>
  
  <p>Let me show what I did in my code.</p>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
      android:layout_width="match_parent" android:layout_height="match_parent"
      tools:context=".MainActivity" tools:ignore="MergeRootFrame" &gt;
  
      &lt;fragment
          android:id="@+id/my_fragment"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.example.zhouhao.test.MyFragment"
          tools:layout="@layout/fragment_my" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p><strong>fragment_my.xml</strong> (my main fragment which include a FragmentTabHost)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@android:id/tabhost"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:weightSum="100"
          android:orientation="horizontal"&gt;
  
          &lt;FrameLayout
              android:id="@android:id/tabcontent"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_weight="0" /&gt;
  
          &lt;LinearLayout
              android:layout_width="0dp"
              android:layout_weight="95"
              android:layout_height="match_parent"
              android:orientation="vertical"&gt;
  
              &lt;FrameLayout
                  android:id="@+id/panel_content"
                  android:layout_width="match_parent"
                  android:layout_height="0dp"
                  android:layout_weight="1" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/LinearLayout&gt;
  &lt;/android.support.v4.app.FragmentTabHost&gt;
  </code></pre>
  
  <p><strong>fragment_tab1.xml</strong> (for the fragment corresponding to different tab, they are similar so that I only show you one code)</p>
  
  <pre><code>&lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" tools:context="com.example.zhouhao.test.TabFragment"&gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
      &lt;TextView android:layout_width="match_parent" android:layout_height="match_parent"
          android:text="@string/fragment_tab1" /&gt;
  
  &lt;/FrameLayout&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong> </p>
  
  <pre><code>protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      if (savedInstanceState == null) {
          FragmentManager fm = getSupportFragmentManager();
          mMyFragment = (MyFragment) fm.findFragmentById(R.id.my_fragment);
      }
  }
  </code></pre>
  
  <p>public class MyFragment extends Fragment implements TabHost.OnTabChangeListener {</p>
  
  <pre><code>FragmentTabHost mFragmentTabHost;
  
  
  public MyFragment() {
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_my, container, false);
      if (rootView != null) {
          mFragmentTabHost = (FragmentTabHost) rootView.findViewById(android.R.id.tabhost);
          mFragmentTabHost.setup(getActivity(), getActivity().getSupportFragmentManager(), R.id.panel_content);
          TabFragment1 tab1Fragment = new TabFragment1();
          TabFragment2 tab2Fragment = new TabFragment2();
          TabFragment3 tab3Fragment = new TabFragment3();
          TabHost.TabSpec spec1 = mFragmentTabHost.newTabSpec("1").setIndicator("");
          TabHost.TabSpec spec2 = mFragmentTabHost.newTabSpec("2").setIndicator("");
          TabHost.TabSpec spec3 = mFragmentTabHost.newTabSpec("3").setIndicator("");
          mFragmentTabHost.addTab(spec1,tab1Fragment.getClass(), null);
          mFragmentTabHost.addTab(spec2,tab2Fragment.getClass(), null);
          mFragmentTabHost.addTab(spec3,tab3Fragment.getClass(), null);
          mFragmentTabHost.setOnTabChangedListener(this);
          return rootView;
      } else {
          return super.onCreateView(inflater, container, savedInstanceState);
      }
  }
  
  @Override
  public void onTabChanged(String tabId) {
  
      BaseFragment f = (BaseFragment)getActivity().getSupportFragmentManager().findFragmentByTag(tabId);
      Log.d("Log",tabId);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>My problem is the BaseFragment is always null in my onTabChanged. Can anybody help? Thanks.</p>