Traif
Traif

Reputation: 711

Activity launches fragment, but fragment doesn't load correctly

I have an Activity with code that launches a Fragment with a replace, but when I load this Fragment, it's not replaced but is displayed above.

I need the fragment to be replaced, not added.

My activity XML:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_height="match_parent">

    <FrameLayout
        android:layout_width="match_parent"
        android:id="@+id/id1"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/idLinear"
            android:background="#f5e5e5e5"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                card_view:cardCornerRadius="0dp">

            //CODE

            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                card_view:cardCornerRadius="0dp">

               //CODE
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </FrameLayout>

</RelativeLayout>

My fragment XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/idtext3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        // CODE
    </RelativeLayout>

    <TextView
        android:id="@+id/idtext2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

       // CODE
    </RelativeLayout>

    <TextView
        android:id="@+id/idtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
        android:paddingLeft="?android:attr/listPreferredItemPaddingLeft"
        android:paddingRight="?android:attr/listPreferredItemPaddingRight"
        android:paddingStart="?android:attr/listPreferredItemPaddingStart"
        android:textAppearance="?android:attr/textAppearanceListItemSmall" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        // CODE
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_marginLeft="@dimen/margenIzquierdo"
        android:layout_height="match_parent">

        // CODE
    </LinearLayout>


</LinearLayout>

My code to launch Fragment:

getSupportFragmentManager().beginTransaction().replace(R.id.id1, object).addToBackStack(null).commit();

Can someone help me?

I need the fragment to be replaced, not added.

Upvotes: 3

Views: 491

Answers (1)

taman neupane
taman neupane

Reputation: 938

I think your issue is with transparent fragment. add this two line inside fragment xml in every fragment in top level.

android:background="@color/white"
android:clickable="true"

second one is when you click on fragment the click event will be taken by activity component tooo. this will prevent the click issue too.

Upvotes: 2

Related Questions