Search code examples
androidandroid-fragmentsnavigation-drawerpreferencefragment

Is it possible to remove a fragment without replacing it by another fragment?


I use an ActionBarActivity with NavigationDrawer and a ListView in the content frame of Drawer.

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/lyt_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ListView
            android:id="@+id/list_notes"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"       >
        </ListView>
    </LinearLayout>
</FrameLayout>

<FrameLayout
    android:id="@+id/drawer_frame"
    android:layout_width="240dp"
    android:layout_height="fill_parent"
    android:layout_gravity="start"
    android:layout_weight="1" >

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/darkGray"
        android:choiceMode="singleChoice"/>
     </FrameLayout>

I open PreferenceFragment from one of Drawer items like this:

  FragmentTransaction transaction = getFragmentManager().beginTransaction();
                PrefsFragment prefs = new PrefsFragment();
                transaction.add(R.id.content_frame, prefs);
                transaction.addToBackStack(null);
                transaction.commit();

and this is my PrefsFragment:

public class PrefsFragment extends Fragment {

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    getActivity().getFragmentManager().beginTransaction()
            .replace(R.id.content_frame, new PrefsFr())
            .commit();
}


public class PrefsFr extends PreferenceFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);          
        addPreferencesFromResource(R.xml.preferences);
      ...  }
}

}

but I can not remove this fragment. When I toggle to another Drawer item; PreferenceFragment remains on top of ListView in content_frame with tranceparent background.

Is it possible in ActionBarActivity to remove a fragment without replacing it by another fragment ? If not so how to use PreferenceFragment for api > 11 ? Is it true that the only way is to replace fragments with each other?


Solution

  • Is it possible in ActionBarActivity to remove a fragment without replacing it by another fragment ?

    Yes, you can use FragmentTransaction#remove(Fragment)

    The underlying problem however is that you are showing the PreferenceFragment with transaction.add(R.id.content_frame, prefs);. If you want only the PreferenceFragment to display wihthout the ListView behind it, you should use replace() instead.