Created
December 31, 2020 13:45
-
-
Save Alireza-Farahani/d776b0780c9f97252e3ea85d3a909796 to your computer and use it in GitHub Desktop.
Overriding back button in fragments was never this easy!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Fragment.overrideBackButtonWith(block: () -> Unit) { | |
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, | |
object : OnBackPressedCallback(true) { | |
override fun handleOnBackPressed() { | |
block() | |
remove() // remove this back button handler | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As an usage example, let's assume we have an Contextual ActionBar and when activiated we want back button to cancel contextual mode