Created
May 4, 2020 20:51
-
-
Save erfanegtfi/40991f3ac078e7a474629fa3f0f8bc66 to your computer and use it in GitHub Desktop.
OnBackPressedCallback in Fragment https://stackoverflow.com/a/54522734/4718406
This file contains hidden or 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
public class MyFragment extends Fragment { | |
@Override | |
public void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
// This callback will only be called when MyFragment is at least Started. | |
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) { | |
@Override | |
public void handleOnBackPressed() { | |
// Handle the back button event | |
} | |
}; | |
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback); | |
// The callback can be enabled or disabled here or in handleOnBackPressed() | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment