I have bottomsheet that check if the internet connected or not! if no connected the bottomsheet showed, if not the bottomsheet dismiss. I used bottomSheetDialog.dismiss();
function to prevent the user from pressing the screen to hide the bottomsheet. Now what I want is that the user if press back when bottomsheet showed exit the app.
not exit bottocheet first and then exit the app
Here's what I made so far
I used an interface called IOnBackPressed, And I Override exit app from "MainActivty" with this code
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_layout);
if (!(fragment instanceof IOnBackPressed)) {
super.onBackPressed();
}
}
And I added the exit app method in the fragment that has the bottomsheet "HomeFragment"
@Override
public boolean onBackPressed() {
bottomSheetDialog.dismiss();
requireActivity().moveTaskToBack(true); //exit the app when press back
requireActivity().finish();
return true;
}
But it is not working, and when I press back it does not exit the app.
Here is my method for bottomsheetdialog
private void showBottomSheetDialog() {
bottomSheetDialog = new BottomSheetDialog(requireContext());
bottomSheetDialog.setContentView(R.layout.bottomsheet_no_internet);
if (CheckNetwork.isInternetAvailable(requireActivity())) {
bottomSheetDialog.dismiss();
} else {
bottomSheetDialog.setCancelable(false);
bottomSheetDialog.show();
}
Button buttonNoInternet = bottomSheetDialog.findViewById(R.id.buttonNoInternet);
assert buttonNoInternet != null;
buttonNoInternet.setOnClickListener(v -> {
if (CheckNetwork.isInternetAvailable(requireActivity())) {
adapter.notifyDataSetChanged();
bottomSheetDialog.dismiss();
} else {
bottomSheetDialog.dismiss();
adapter.notifyDataSetChanged();
bottomSheetDialog.show();
}
});
}
So how can I do that?