16

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

Zetch
  • 163
  • 1
  • 1
  • 5

3 Answers3

14

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"
Nik
  • 7,114
  • 8
  • 51
  • 75
  • 15
    Hmm I'm having the same issue myself, but surely this answer is just masking the problem and the fragments should be stopped rather than having a reference held onto and just hidden by a background colour – Daniel Wilson Jun 10 '14 at 16:08
  • 5
    I'm guessing that this will add over draw, making your ui animation less fluid. This doesn't solve your problem but literally hides it. – MinceMan Oct 24 '14 at 20:57
2

I tried a lot of suggested answers here on SO and after expending hours I finally could solve my problem without having to specify a background color on my fragment's layout.

My solution is to override the onBackPressed method on the Activity holding the fragment container, so every time the back button is pressed I will remove the current shown fragment and bring back the previous one:

@Override
public void onBackPressed() {
    List<Fragment> fragments = getSupportFragmentManager().getFragments();
    if (fragments.size() > 1) {

        final Fragment currentFragment = getSupportFragmentManager().findFragmentById(R.id.my_framelayout);
        if (currentFragment != null) {
            getSupportFragmentManager().beginTransaction().remove(currentFragment).commit();
            getSupportFragmentManager().popBackStack();
        }
    } else {
        super.onBackPressed();
    }
}

Hope this helps.

Pedro Hidalgo
  • 861
  • 10
  • 15
0

Try to change the android:background="@android:color/background_light" color to some other color.May be this solves your problem.

VK.Dev
  • 801
  • 4
  • 11
  • 24
  • I've tested to change to a static color like ´android:background="#F00"` or using system colors references but the result is the same. Both fragments are displayed and they don't respond to input. – Zetch May 27 '13 at 12:20
  • Refer this page http://developer.android.com/training/implementing-navigation/nav-drawer.html Try to download the sample project and compare it with your existing project. Try to remove ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); this line and run. – VK.Dev May 27 '13 at 12:55
  • Ok, I've tried setting background color on every Frame and every Fragment layout and it seems to work ok. But now I have another problem, every loaded fragment load once ok but if I load them again, ListViews are empty. Every ListView has an ArrayAdapter which is loaded when `onActivityCreated` of every Fragment is called. – Zetch May 27 '13 at 14:40
  • I've tried again changing extending ListFragment instead using Fragment with custom layout and it works. Anyone knows which method do I have to call for auto refreshing using Fragment classes? – Zetch May 27 '13 at 15:48