When changing Fragment, the previous one is visible below the new one

Asked

Viewed 1,372 times

1

I’m using Fragments instead of Activity, only when I call the second Fragment with the replace method, it doesn’t completely overlap the previous Fragment, it’s on top, but the drawing of the other Fragment keeps appearing, as I do so it doesn’t happen ?

xml code from the first screen

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

<fragment
    android:id="@+id/primeiro_fragment"
    android:name="br.com.demmy.demet.gerador.PrimeiroFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</fragment>
</FrameLayout>

Code of the first fragment.java

public class PrimeiroFragment extends Fragment {

View minha_view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup       container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.tela_principal, container, false);
    return minha_view;
}

}

main screen code.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/frame_layout"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

<LinearLayout
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        android:id="@+id/textView_hora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:textSize="18sp"
        android:text="texto1" />

    <EditText
        android:id="@+id/editText_hora"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/oval"
        android:padding="8dp"
        android:layout_margin="8dp"
        android:ems="4"
        android:inputType="number" />

    <TextView
        android:id="@+id/textView3"
        android:layout_gravity="center"
        android:textSize="18sp"
        android:layout_marginTop="3dp"
        android:layout_marginBottom="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Texto 2" />

    <EditText
        android:id="@+id/texto_notification"
        android:padding="14dp"
        android:gravity="start"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="8dp"
        android:layout_weight="1"
        android:background="@drawable/oval"
        android:inputType="textMultiLine" />
    <Button
        android:id="@+id/button"
        style="@style/estilo_botao"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="Cadastrar" />
</LinearLayout>
</FrameLayout>

Code of the second fragment.java

public class SegundoFragment extends Fragment {

View minha_view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    minha_view = inflater.inflate(R.layout.segundo_layout, container, false);
    return minha_view;
}

}

code of the second layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="138dp"
    android:layout_marginStart="138dp"
    android:layout_marginTop="196dp"
    android:text="SEGUNDO LAYOUT" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/textView2"
    android:layout_marginLeft="68dp"
    android:layout_marginStart="68dp"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/textView2"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/button2"
    android:layout_marginTop="16dp"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button3"
    android:layout_alignParentTop="true"
    android:layout_alignStart="@+id/button3"
    android:layout_marginLeft="17dp"
    android:layout_marginStart="17dp"
    android:layout_marginTop="60dp"
    android:text="Button" />

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/textView2"
    android:text="Button" />
</RelativeLayout>

code that is calling the Fragments

lista_drawer.setOnItemClickListener(new AdapterView.OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            android.app.FragmentManager fragmentManager = getFragmentManager();
            if (position == 0){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new PrimeiroFragment()).commit();
            }else if(position == 1){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new SegundoFragment()).commit();
            }else if(position == 2){
                fragmentManager.beginTransaction().replace(R.id.frame_layout, new TerceiroFragment()).commit();
            }
        }
    });

Como está ficando o app quando chamo o segundo fragment

  • If you put android:background="@android:color/white" in the RelativeLayout of the second Fragment can solve this problem.

  • It is but this way, the components of the previous screen still remain on the screen, only invisible

  • is true, has this problem. If you also put android:clickable="true" in the layout of the second Fragment the components of the first Fragment will not be clickable at least.

  • 1

    @That’s like "sweep the garbage under the rug".

2 answers

1


I think the problem is mixing statically added Fragments with dynamically added.
That block <fragment/> should not be in "first screen xml code".

XML to be used by Activity(replaces "first screen xml").

Activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="br.com.demmy.demet.gerador.MainActivity"
tools:showIn="@layout/app_bar_main">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</LinearLayout>

onCreate() of Activity:

@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frame_layout, new PrimeiroFragment());
    ft.commit();

    -----
    -----
}
  • It worked out man !!! thank you very much, it’s always you who helps me, it’s kind of weird, but all right... vlw ai

  • It’s "kind of weird," because?

  • why, there are so many people who use the site, but only you who respond...

  • I put all Fragments to be opened at runtime and it worked, however, now gives an error when I try to access the Edit_text from the main screen by the findViewById method, some hint ?

  • You must take into account that you can only access views, through findViewById(), that are in the Activity layout (assigned by setContentView()) or in the "inflated" layout on onCreateView() of a Fragment.

1

This happens because just put a new fragment instead of replacing him. The best method is always to assign a fragment to a particular layout and then always replace it in the same layout.

You can do it as follows:

FragmentManager manager = getSupportFragmentManager();
                    fragment_name fragment_name = new fragment_name();
                    manager.beginTransaction().replace(R.id.layoutasubstituir, fragment_name, fragment_name.getTag()).commit();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.