Fragment is not overriding the marked layout

Asked

Viewed 487 times

3

I’m working on a college project and I’m having a hard time replacing a given layout by a fragment. What is happening is that, the fragment is not replacing the layout of Activity but yes, mixing with it.

This is the code block that calls the fragment:

    botaoDeFalar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Bundle bundle = new Bundle();
            String textoParaFragment = editTextPrincipal.getText().toString();
            bundle.putString("oqSeraFalado", textoParaFragment);
            TesteFragment testefragment = new TesteFragment();
            testefragment.setArguments(bundle);
            getSupportFragmentManager().beginTransaction().replace(R.id.LayoutMain, testefragment).commit();
            oqSeraFalado = textoParaFragment;
            vamosFalar();
        }
    });
}

The variable BotaoDeFalar is an element of layout Mainactivity(button) Which To Tight, Runs All This Block.

ID Layoutmain, is the layout which includes all other elements of Activity.

<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="com.example.mikha.projetointegrador5android.MainActivity"
    android:id="@+id/LayoutMain">

This is the Java of Fragment in question:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_teste, container, false);
    resultadoPalavra = (TextView) view.findViewById(R.id.TextViewTesteFragment);
    palavraDigitada = this.getArguments().getString("oqSeraFalado");
    resultadoPalavra.setText(palavraDigitada);
    return view;

}

The result of this when I run the application is that instead of Fragment replace everything we see on the main screen with XML’s Fragment, it adds the XML of Fragment with the main screen XML, as if merged.

1 answer

0

It is not possible to replace the layout of Activity with that of Fragment.

In terms of layout, what the method replace() replaces the layout of a Fragment, previously added to the specified view group, with this.
If no Fragment has been added to the specified view group, the method adds this and includes its layout in the view group.

So that when adding a Fragment your layout is not merged with the other content it should be added to an empty group view included in the Activity layout, usually a Framelayout.

So, if you want to replace all the contents of the Activity layout, this layout should have only one Framelayout with nothing inside, then use two Fragments, the one you already have and the other for the content you had Activity.

  • Would it be as if I used a blank screen in Main Activity and was playing on it, the content I wanted separated by Fragments? hence, if I wanted to continue following what qro, then I should use an Intent instead of Fragment. right?

  • Yes, that’s about it. If you don’t want to use two Fragments use two activities. Replace the Fragment you have with an Activity and instead of getSupportFragmentManager().beginTransaction().replace() use startActivity()

  • I get it. but explain me one thing... I redid the code, put the element of the Mainactivity XML layout that will be replaced by Fragment, inside a Framelayout. (in this case, it is an Edittext). But it still does not replace! inside the main screen XML, has 1 layout frame and inside it an Edittext. the user types something in Edit text, presses the button and Fragment is called in place of this Frame layout. It continues not replacing, just merging =\

  • You have to name this Framelayout and use it on getSupportFragmentManager().beginTransaction().replace(). Framelayout should have nothing inside.

  • replace the contents of Framelayout only if it has been placed by another Fragment.

Browser other questions tagged

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