Fragment is shown superimposed on an Activity

1

I do not end up with a problem because everyone is left unresolved, and now I find another, which I do not understand because it is happening, since I have seen other codes and it works correctly.

The problem is that I have put a button in a class that extends from AppCompatActivity , to call from that button a class that extends Fragment , by pressing the button the Fragment is superimposed, seeing both.

This is one of the codes I've tried and I have put in Activity to show the Fragment

Alarmas.java

public class Alarmas_Login extends AppCompatActivity

Code to show the Fragment

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container_login, new Alarmas());
transaction.addToBackStack(null);
transaction.commit();

alarms_login.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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"
tools:context=".Alarmas_Login">


<EditText
    android:id="@+id/intro_login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="56dp"
    android:ems="10"
    android:hint="Introducir Contraseña"
    android:inputType="textPassword"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.502"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/bt_aceptar"
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="12dp"
    android:text="Aceptar"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/intro_login" />

<FrameLayout
    android:id="@+id/container_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
</FrameLayout>


</android.support.constraint.ConstraintLayout>

I would appreciate your help if someone can tell me how to show only one class.

Greetings.

    
asked by SoCu 07.12.2018 в 06:10
source

2 answers

0

The problem is that you are using a ContraintLayout , but the FrameLayout child does not have the correct restrictions: it says that it will occupy all the available space without restrictions (that is, the whole screen).

Solution : Set the restrictions to FrameLayout ; for example:

<FrameLayout
    android:id="@+id/container_login"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/bt_aceptar"
    app:layout_constraintBottom_toBottomOf="parent" >
</FrameLayout>
    
answered by 12.12.2018 в 14:47
0

The Framelayout container_login exists, the problem is that this element does not have defined position "constraints"

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    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"
    tools:context=".Alarmas_Login">

...
...

    <FrameLayout
        android:id="@+id/container_login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </FrameLayout>


    </android.support.constraint.ConstraintLayout>

you must add them, for example:

    <FrameLayout
        android:id="@+id/container_login"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bt_aceptar">
    </FrameLayout>

I suggest you remove transaction.addToBackStack (null);

addToBackStack () Add the transaction to the "Back stack". This means that the transaction will be remembered after it is committed, and will revert its operation when it is removed from the stack.

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.container_login, new Alarmas());
//transaction.addToBackStack(null);
transaction.commit();
    
answered by 12.12.2018 в 14:17