How to disable back button pressed in android fragment class
You have to override onBackPressed of parent FragmentActivity class. Therefore, put your codes in parent FragmentActivity. Or you can call parent's method by using this:
public void callParentMethod(){
getActivity().onBackPressed();
}
in FragmentActivity override onBackPressed Method and not call its super class to disable back button.
@Override
public void onBackPressed() {
//super.onBackPressed();
//create a dialog to ask yes no question whether or not the user wants to exit
...
}
Here is the new way you can manage your onBackPressed()
in fragment with the new call back of activity:
// Disable onBack click
requireActivity().onBackPressedDispatcher.addCallback(this) {
// With blank your fragment BackPressed will be disabled.
}
Here is the android doc link: https://developer.android.com/reference/androidx/activity/OnBackPressedDispatcher