Skip to content
Advertisement

How to exit the app when bottomsheetdialog showing

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?

Advertisement

Answer

By default when the back key pressed while a BottomSheetDialog is showing, the activity’s onBackPressed() won’t be called.. You probably can try to put a break point inside onBackPressed() and see.

So, a workaround other than onBackPressed() is to attach a Key listener to the BottomSheetDialog and check the code of the pressed key that matches the back code; and there you can dismiss the dialog and exit the app:

bottomSheetDialog = new BottomSheetDialog(requireContext()) {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
                    // Back key is pressed

                    bottomSheetDialog.dismiss(); // Optional
                    requireActivity().moveTaskToBack(true); //exit the app when press back
                    requireActivity().finish();
                    return true;
                }
                return false;
            }
        });

        Button buttonNoInternet = findViewById(R.id.buttonNoInternet);
        buttonNoInternet.requestFocus();

    }

};

UPDATE:

When I try this code I MUST click on tryAgain Button and then I can exit the app. But it is not exit the app when BottomSheet showen

So, The reason that the dialog is shown earlier than the BottomSheetFragment is shown on the screen, so, you need to show it when the the BottomSheetFragment is shown on the screen:

So this part of code need to be transferred to BottomSheetFragment onResume() method:

@Override
public void onResume() {
    super.onResume();
    if (CheckNetwork.isInternetAvailable(requireActivity())) {
        bottomSheetDialog.dismiss();
    } else {
        bottomSheetDialog.show();
    }
}

And you can just setup the DialogFragment like said in onCreate() method of the BottomSheetFragment

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement