Handling back press when using fragments in Android

前端 未结 5 899
一整个雨季
一整个雨季 2021-01-30 12:00

I am using Android Sliding Menu using Navigation Drawer in my application and Fragments are used in the app instead of Activities. When I open the drawer, click on an item a Fra

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 12:34

    use this (in kotlin)

    activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
        override fun handleOnBackPressed() {
            // in here you can do logic when backPress is clicked
        }
    })
    

    i think this is the most elegant way to do it

提交回复
热议问题