Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment replaced still visible on background

I'm trying to replace one fragment with another one using new navigation drawer pattern. It seems to work but when I choose another option from drawer the new fragment is loaded but both fragments are visible. I'm not using static fragment layout so I don't know where is the problem.

Fragments are loaded through onItemClick method implementing AdapterView.OnItemClickListener on my activity.

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Fragment fragmentToShow = null;
    // Load desired fragment
    switch (position) {
        case 0: // Authors
            if (fragmentAuthors == null) fragmentAuthors = new FragmentAuthors();
            fragmentToShow = fragmentAuthors;
            break;
        case 1: // Books
            if (fragmentBooks == null) fragmentBooks = new FragmentBooks();
            fragmentToShow = fragmentBooks;
            break;
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    ft.replace(R.id.ActivityMain_Drawer_FrameMain, fragmentToShow);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    ft.commit();
    mDrawerLayout.closeDrawers();
}

Layout

<!-- The main content view -->
<FrameLayout
    android:id="@+id/ActivityMain_Drawer_FrameMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light" />

<!-- The navigation drawer -->
<FrameLayout
    android:id="@+id/ActivityMain_Drawer_FrameMenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@android:color/background_light">
    <ListView
        android:id="@+id/ActivityMain_Drawer_FrameMenu_List"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />
</FrameLayout>

Screenshots

https://dl.dropboxusercontent.com/u/1763308/Screenshots/device-2013-05-27-113128.png https://dl.dropboxusercontent.com/u/1763308/Screenshots/device-2013-05-27-113139.png

like image 678
Zetch Avatar asked May 27 '13 10:05

Zetch


People also ask

How do I destroy fragments after replace?

To remove (so destroy) these fragments, you can call: You need to use findFragmentById for Fragment from XML file. And then find this in the MainActivity on click listener. I've added this to the OnClickListener but it always returns 'null' for the fragment I want to remove.

How do you stop a fragment from recreating?

Calling setRetainInstance(true) will prevent the Fragment from being re-created.

Which method is called when fragment is replaced?

Use replace() to replace an existing fragment in a container with an instance of a new fragment class that you provide. Calling replace() is equivalent to calling remove() with a fragment in a container and adding a new fragment to that same container. transaction. commit();

What happens to fragment when activity is destroyed?

As Fragment is embedded inside an Activity, it will be killed when Activity is killed. As contents of activity are first killed, fragment will be destroyed just before activity gets destroyed.


1 Answers

I have same issue with some fragments. To solve it I simple set background color for all fragment layouts, for example:

android:background="?android:attr/colorBackground"
like image 197
Nik Avatar answered Nov 08 '22 11:11

Nik