-1

I want to go from fragment_profile to fragment_details while clicking the details button that is found in the fragment_profile. Here is the xml of the button

    android:id="@+id/btn_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:onClick="onClickDetail"
    app:prof_icon="@string/fa_user"
    app:prof_title="Personal Info" />

Now I'm having some difficulties with the onClickDetail function because I don't know how to make the passage from fragment_profile to fragment_detail.

Here is what I've tried so far

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    btn_details.setOnClickListener(View.OnClickListener() {
        fun onClickDetail(view: View?) {
            val myIntent = Intent(activity, DetailFragment::class.java)
            activity?.finish()
            activity?.startActivity(myIntent)
        }
    }
    )
}
MKK
  • 49
  • 7

1 Answers1

3

You should open fragment via fragment manager like that:

activity!!
    .supportFragmentManager
    .beginTransaction()
    .replace(R.id.container, DetailFragment())
    .commitNow()
Roman Shtykalo
  • 185
  • 1
  • 5
  • Hi. Im sorry but I'm pretty new with this things, How do I use this? – MKK Aug 24 '20 at 10:52
  • override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) btn_details.setOnClickListener(View.OnClickListener() { fun onClickDetail(view: View?) { activity!! .supportFragmentManager .beginTransaction() .replace(R.id.container, DetailFragment()) .commitNow() } } ) } @MKK – Roman Shtykalo Aug 24 '20 at 17:13