0

I'm new in creating fragments and I have little problem with it. In second activity (in layout) I have widget

<fragment 
...
android:name="path to my first fragment"
...
/>

I have three fragments to swap. I make it in this way

  // get fragment manager and fragment transaction
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();

        // replace fragment on register form
        transaction.replace(R.id.fragment, new Confirmation());

        // commit transaction
        transaction.commit();

and my fragments layout is wrap in two constraint layout like this

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
// additional widgets
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    </android.support.constraint.ConstraintLayout>

I found this solution on stack because I had visible remains of previous fragment. Now I have invisible reamins of previous fragment. Where I have a mistake? When I click on the position of button from previous fragment the button on this location is pressed

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • `Fragment`s defined in your layout cannot be removed/replaced at runtime. Change the `` to a regular ``, and load the `Fragment` that was there into it at startup. – Mike M. Jul 29 '18 at 08:55
  • Also, I'm not sure how the doubled-up layout was helping, but you likely don't need to do that anymore. – Mike M. Jul 29 '18 at 09:11
  • @Mike M. thank you for help, it works, but I don't know when should be used? I couldn't find this answer that you marked :/ – Michał Urban Jul 29 '18 at 09:31
  • You can think of `` as kind of a permanent fixture in your layout; like a special kind of `View`. If you don't need to dynamically remove/replace a `Fragment` at runtime, that's what you would use. It's useful when you want to reuse some UI elements, along with their backing logic, in separate components or projects, but you don't ever need to swap them out while running. They can make life a little easier, as far as dealing with the lifecycle – e.g., during a rotation – because the `FragmentManager` automatically handles loading them, and keeping track of them, for you. Make sense? – Mike M. Jul 29 '18 at 09:38
  • The linked duplicate is in a banner now at the top of your question. You might need to refresh the page. Here's the link again, just in case: https://stackoverflow.com/questions/29788837/android-fragment-view-is-not-removed. – Mike M. Jul 29 '18 at 09:41
  • 1
    Thank you, now I understand :) – Michał Urban Jul 29 '18 at 10:00

0 Answers0