Home > Net >  How can I switch from a fragment to another fragment in Kotlin?
How can I switch from a fragment to another fragment in Kotlin?

Time:01-08

I want the fragment_yetki.xml file to be opened when the yetki_modulu_cardView in the fragment_mudur_home.xml is clicked. How can I do this from within the MudurHomeFragment.kt class?

MudurHomeFragment.kt

fragment_home_mudur

CodePudding user response:

For that first you have to create an action in the mobile_navigation page. Like this

`<fragment
        android:id="@ id/titleFragment"
        android:name="com.dheeraj.guessbro.title.TitleFragment"
        android:label="title_fragment"
        tools:layout="@layout/title_fragment">
       **<action
            android:id="@ id/action_titleFragment_to_gameFragment"
            app:destination="@id/gameFragment" />**
    </fragment>'

and then go to the MudurHomeFragment.kt file and just use this code :

binding.yetki_modulu_cardView.setOnClickListener { findNavController(this).navigate(R.id.action_titleFragment_to_gameFragment) }

If you are not using view binding then you can first define the variable using findViewById and then use the clickListener.

  •  Tags:  
  • Related