Search code examples
androidandroid-fragmentsfragment-tab-hostfragmenttransactionfragmentmanager

Tab widget remains when replacing a FragmentTabHost in its container


I have a MainActivity in which I set the content of a FrameLayout with id container to a FragmentTabHost. In the content of one tab containing a ListView I want to replace the whole content of container by a new Fragment (i.e. removing the whole FragmentTabHost), but the tab widgets remains and only the tab content is replaced.

Why is the FragmentTabHost not completely replaced? How can I remove it?

enter image description here

MainActivity.java

public class MainActivity extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      

        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        Fragment notificationTabsFragment = new NotificationTabsFragment();
        fragmentTransaction.replace(R.id.container, notificationTabsFragment, "notificationTabsFragment");
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}

main.xml

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

    <fragment
        android:id="@+id/header"
        android:name="com.xxx.fragment.HeaderFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

NotificationTabsFragment

public class NotificationTabsFragment extends Fragment {

    private FragmentTabHost mTabHost;

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

        mTabHost = new FragmentTabHost(getActivity());
        mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.container);       

        mTabHost.addTab(
                mTabHost.newTabSpec("all").setIndicator(getString(R.string.all), null),
                NotificationListFragment.class, null);

        /* adding more tab */ 

        return mTabHost;
    }
}

NotificationListFragment.java

public class NotificationListFragment extends Fragment {

    private ListView listview;
    private NotificationAdapter adapter;

    private List<Notification> notifList;

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

        notifList = MyApplication.dbHelper.getNotificationList();

        View view = inflater.inflate(R.layout.notification_list, container, false);

        listview = (ListView) view.findViewById(R.id.listview);
        listview.setEmptyView(view.findViewById(R.id.empty));

        adapter = new NotificationAdapter(getActivity(), notifList);
        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapter, View v, int position,
                    long arg3) 
            {
                Bundle args = new Bundle();
                args.putLong(Notification.KEY_OCC_ID, notifList.get(position).occ_id);

                Fragment fragment = new NotificationFragment();
                fragment.setArguments(args);
                FragmentTransaction transaction = getFragmentManager().beginTransaction();

                transaction.replace(R.id.container, fragment); // replacing the whole container, but the tab widgets remain!
                transaction.addToBackStack(null);

                transaction.commit();
            }
        });

        return view;
    }
}

NotificationFragment

public class NotificationFragment extends Fragment {

    private TextView text;

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

        View view = inflater.inflate(R.layout.notification_detail, container, false);
        text = (TextView) view.findViewById(R.id.dude);

        text.setText("NotificationFragment content");

        return view;
    }

}

Solution

  • In the onCreateView() of your NotificationListFragment, replace

    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    

    with either this:

    FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
    

    or this:

    FragmentTransaction transaction = getParentFragment().getFragmentManager().beginTransaction();
    

    Try this. It should work.