Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments displayed over each other

I've got an Activity with a DrawerLayout, using the guidelines from http://developer.android.com/training/implementing-navigation/nav-drawer.html.

When I click on an drawerItem, I replace the current view with the new fragment:

Fragment fragment;
Bundle args = new Bundle();    
fragment = new NewsListFragment();
args.putInt("category", position);

// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();        
mDrawerList.setItemChecked(position, true);

Now, sometimes the old fragment is not replaced but the new fragment is placed on top of the old one:

http://img15.imageshack.us/img15/3179/1kqj.png

Why is this, and how to solve this problem?

Relevant XML:

<!-- The main content view -->
<LinearLayout 
    android:id="@+id/rlMain"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    android:layout_height="fill_parent">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" 
        android:layout_weight="1">
    </FrameLayout>

This only happens sometimes, and I haven't found a flow to reproduce this yet. The app doesn't support rotating, so it won't happen there.

like image 585
aaronsw Avatar asked Aug 19 '13 08:08

aaronsw


People also ask

Where does a fragment live in an activity?

When a fragment is part of the activity, it lives in a ViewGroup inside the activity's view hierarchy and the fragment defines its own view layout. In this example, I’m displaying the fragments inside a FrameLayout.

How to display 2 fragments in the activity in Android Studio?

In Android Studio, Right click on main source set folder, click on New -> Activity -> Empty Activity. Now you have created the base activity. After creating the activity, we need to edit the xml file. In this blog, I’ll show How to display 2 fragments in the activity.

How to add fragments on top of each other?

Let's say that you have the root fragment A which you add to a container. Next you add fragment B and you set addToBackStack in the transaction. Lastly you add fragment C but you omit addToBackStack. Now when you press the back button you get these fragments on top of each other. You could solve this issue by also adding C to the backStack.

What is a fragment in Java?

The fragment is something like Frame in Html. One Html can contain multiple Frames, and one Activity can include multiple Fragments. Each Fragment can have its own layout XML file, can contain its own view objects and encapsulate its own functionality. It can be reused by any activity.


2 Answers

We went live with this version and havent received any complaints about this, so I will assume this was the correct answer:

in your onCreateView method add:

if (container != null) {     container.removeAllViews(); } 

Be sure to check if container is not null!

Thanks https://stackoverflow.com/users/2677588/lia-pronina!

like image 59
aaronsw Avatar answered Sep 29 '22 04:09

aaronsw


After about 1 week, I found the solution without adding background color or anything else. Just add this code and fix that bullshit. I hope it will help all of you.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    container.clearDisappearingChildren();
    return inflater.inflate(R.layout.fragment, container, false);
}
like image 28
Görkem Bendin Avatar answered Sep 29 '22 03:09

Görkem Bendin