Reputation: 657
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?
Upvotes: 2
Views: 2151
Reputation: 40830
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
Upvotes: 2
Reputation: 1303
The reason it's not working is because the BottomSheetDialog
is a Dialog
which has it's own onBackPressed()
.
You need to override the following in your BottomSheetDialog
:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new Dialog(getActivity(), getTheme()){
@Override
public void onBackPressed() {
// Maybe check before if there still no connection
requireActivity().moveTaskToBack(true);
requireActivity().finish();
}
};
}
Upvotes: 1
Reputation: 1324
I am not sure about your back stack state. Is it the last Activity on that stack that is showing the Bottom Sheet Dialog? If it is then you can use a callback function that will call finish()
on that activity. If it not, you can use this reference that states
You can exit from the activity using the following code (But this will not kill the underlying activities in the same application. This will just minimize the application)
var intent = new Intent(Intent.ActionMain);
intent.AddCategory(Intent.CategoryHome);
intent.SetFlags(ActivityFlags.NewTask);
startActivity(intent);
finish();
Killing the process
android.os.Process.killProcess(android.os.Process.myPid())
Upvotes: 1
Reputation: 111
Below code will work as expected, I'll share you my code snippet.
Inside MainActivity I have below code for exit the app
@Override
public void onBackPressed() {
super.onBackPressed();
}
Inside fragment I have a bottomsheet dialog. Please note my MainActivity consist of fragment
Code for bottomsheet dialog
private void openDialogCategoryList() {
BottomSheetDialog dialog = new BottomSheetDialog(mActivity, R.style.DialogStyle);
BottomSheetDialogLayoutCountryCodeBinding binding = DataBindingUtil.inflate(LayoutInflater.from(mActivity),
R.layout.bottom_sheet_dialog_layout_country_code, null, false);
dialog.setContentView(binding.getRoot());
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.getWindow().findViewById(R.id.design_bottom_sheet).setBackgroundResource(android.R.color.transparent);
dialog.setCancelable(false);
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
@Override
public boolean onKey(DialogInterface arg0, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().onBackPressed();
}
return true;
}
});
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
countProductType = 0;
}
});
binding.txtTitle.setText("Select Product Type");
binding.edtSearch.setHint("Search Product Type");
binding.txtClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
binding.flOpenClose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
Feel free to ask if you have doubt
Upvotes: 2
Reputation: 422
Try with this code
Intent i = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
or
System.exit(0);
or
finishAffinity();
finish();
Upvotes: 0