I am creating a MainActivity. This activity has a button to open a Fragment and the Fragment has a button to open a bottom sheet dialog.
When I am on the Fragment, I can press Back button to return to MainActivity. However, when I have already opened the Bottom Dialog, I want to disable the Back button so that user can not press Back button when the Bottom Dialog is showing. So how can I do this? Thank you.
MainActivity
public class MainActivity extends SdwBaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void startAction(View view){
LoadCashTutorialScreen loadCashTutorialScreen = LoadCashTutorialScreen.newInstance();
getSupportFragmentManager().beginTransaction().replace(R.id.container, loadCashTutorialScreen, "LoadCashTutorialScreen").addToBackStack("LoadCashTutorialScreen").commit();
}
@Override
public void onBackPressed(){
super.onBackPressed()
//do something so that the back button is disable when the Bottom Dialog is showing
}
}
Fragment:
public class LoadCashTutorialScreen extends Fragment{
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = initView(inflater, container, R.layout.load_cash_tutorial_screen);
Button startDialog = view.findViewById(R.id.dialogButton);
startDialog.setOnClickListener(view1 -> {
MyBottomDialog dialog = new MyBottomDialog();
dialog.show(activity.getSupportFragmentManager(), "BottomDialog");
});
return view;
}
}
Dialog:
public class MyBottomDialog extends BottomSheetDialogFragment {
@Override
public void setupDialog(@NonNull Dialog dialog, int style) {
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.load_cash_bottom_dialog, null);
dialog.setContentView(contentView);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
}
}
Note: I used to write some methods on onBackPressed() to prevent dialog disappears, however, the dialog always disappear when pressing Back. After debugging, I realize that onBackPressed() is not reached when the bottom dialog is showing. I do not know why.
Since your Bottom sheet is a Fragment
, you have to listen it in a separate way. Use this:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
// ...
setupBackPressListener()
}
private fun setupBackPressListener() {
this.view?.isFocusableInTouchMode = true
this.view?.requestFocus()
this.view?.setOnKeyListener { _, keyCode, _ ->
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Do what you want to do on back press
true
} else
false
}
}
This will override default onbackpress of bottom sheet fragment, So, if you add this to bottomsheet fragment and leave it empty nothing will happen when you press back button.
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return object : BottomSheetDialog(requireContext(), theme) {
override fun onBackPressed() {
// Handle backpress in here
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With