How to handle back button when at the starting destination of the navigation component

后端 未结 8 1703
盖世英雄少女心
盖世英雄少女心 2021-01-02 05:01

I\'ve started working with the new navigation component and I\'m really digging it! I do have one issue though - How am I supposed to handle the back button when I\'m at the

8条回答
  •  醉话见心
    2021-01-02 05:26

    You shouldn't override "onBackPressed", you should override "onSupportNavigateUp" and put there

    findNavController(this, R.id.my_nav_host_fragment)
                .navigateUp()
    

    From the official documentation: You will also overwrite AppCompatActivity.onSupportNavigateUp() and call NavController.navigateUp

    https://developer.android.com/topic/libraries/architecture/navigation/navigation-implementing

提交回复
热议问题