I have two activities. from the first one, I call the second one, and from the second one, I call a fragment by pressing a button inside the second activity, but when I click on the button in the first activity to trigger the second activity, a fragment is immediately called
the code for calling the second activity
val chooseActivityButton = findViewById<Button>(R.id.GoActivity) chooseActivityButton.setOnClickListener { val intent = Intent(this, chooseLab::class.java) // start your next activity startActivity(intent) }
the code for calling the fragment
fun onClick(view: View){ setContentView(R.layout.activity_choose_lab); val callFragmentFirst = findViewById<Button>(R.id.call_fragment_first) callFragmentFirst.setOnClickListener { val fragmentTransaction: FragmentTransaction = supportFragmentManager.beginTransaction() fragmentTransaction.replace(R.id.FirstFragmentContainer, FirstFragment()) // или .replace(R.id....) fragmentTransaction.commit() } }
xml fragment conteiner
<androidx.fragment.app.FragmentContainerView android:id="@+id/FirstFragmentContainer" android:name="com.example.myapplication.FirstFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />