简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM