Search code examples
androidandroid-fragmentsandroid-tabhostfragment-tab-host

Tab always selected in TabHost


I have a problem with my TabHost, inside a Fragment, the first tab to be selected will always be highlighted, so I have often two tabs selected as shown here

first image, notifications tabs is highlighted when it should not

Here Notifications should not be highlighted !

But the problem is only for Notifications tab, as you can see here the tab friends behave just normally, because it is the second added..

Secend picture here, friends works normally

Here is my code for the fragment holding my TabHost

public class ProfileContentFragment extends Fragment {
private FragmentTabHost mTabHost;
public ProfileContentFragment() {
}

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.profile_content_fragment,container, false);


    mTabHost = (FragmentTabHost)     rootView.findViewById(android.R.id.tabhost);
    mTabHost.setup(getActivity(), getFragmentManager(),      android.R.id.tabcontent);

              mTabHost.addTab(mTabHost.newTabSpec("notifications").setIndicator("Notifications    "),
            NotificationsFragment.class, null);

    mTabHost.addTab(mTabHost.newTabSpec("friends").setIndicator("Friends"),
            FriendsFragment.class, null);

    return rootView;
}

@Override
public void onDestroyView(){
    super.onDestroyView();;
    mTabHost=null;
}
}

And here is the XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">


        <LinearLayout
            android:id="@+id/LinearLayout01"
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_height="wrap_content"
                android:layout_width="fill_parent">
            </TabWidget>

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </FrameLayout>

        </LinearLayout>


</android.support.v4.app.FragmentTabHost>
</RelativeLayout>

Solution

  • SOLVED :

    It was solved by changing

     mTabHost.setup(getActivity(), getFragmentManager(),     android.R.id.tabcontent);
    

    To :

    mTabHost.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);