Skip to content

Instantly share code, notes, and snippets.

@gokmenbayram
Last active July 5, 2021 13:06
Show Gist options
  • Save gokmenbayram/4097c4c72c37ac3844fbed0c5ed8065a to your computer and use it in GitHub Desktop.
Save gokmenbayram/4097c4c72c37ac3844fbed0c5ed8065a to your computer and use it in GitHub Desktop.
OnBackPressedFrament.kt
import android.content.Context
import androidx.activity.OnBackPressedCallback
import androidx.annotation.NonNull
import androidx.fragment.app.Fragment
class OnBackPressedFrament : Fragment() {
override fun onAttach(@NonNull context: Context) {
super.onAttach(context)
val callback = object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
// do something.
}
}
requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner, callback)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment