I want to disable the back button in a fragment class. onBackPressed() doesn\'t seem to work in this fragment. How could I disable the back button?
This
Here is the code which you can write in your Fragment class to customize the back button press.
public class MyFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
OnBackPressedCallback callback = new OnBackPressedCallback(true /* enabled by default */) {
@Override
public void handleOnBackPressed() {
// Handle the back button even
Log.d("BACKBUTTON", "Back button clicks");
}
};
requireActivity().getOnBackPressedDispatcher().addCallback(this, callback);
}
}
You can read and research more on this HERE