0

I'm using MvRx and trying to understand a concept by instantiating two instances of a fragment on one screen. I think the problem is in my XML as no matter what I do, I only get one instance of the fragment. Any help would be awesome.

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    android:id="@+id/container"
    tools:context=".MainActivity">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container1"
        >

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

    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt:

package com.example.mvrxtutorial

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        if (savedInstanceState == null) {
            supportFragmentManager.beginTransaction()
                .replace(R.id.container1, MainFragment())
                .replace(R.id.container2, MainFragment())
                .commit()
        }
    }
}
lanierc
  • 182
  • 2
  • 12
  • 1
    since both `FrameLayout` has set width/height to `match_parent`, dont they cover each other ? thats why u see only one fragment – P.Juni Oct 20 '20 at 13:42
  • duh, that's gotta be it. Thanks! – lanierc Oct 20 '20 at 13:59
  • Also, be careful about using `match_parent` with `ConstraintLayout`. It will "work" sometimes, but then can break on you if you ever touch the view hierarchy at runtime. The correct way to do it is to set the width/height to `0dp` and then add constraints on both sides to the parent. – Ben P. Oct 20 '20 at 15:16

1 Answers1

0

since both FrameLayout has set width/height to match_parent, dont they cover each other ? thats why u see only one fragment

P.Juni
  • 2,365
  • 14
  • 26