要达到上述目的,可以在您的Activity的onBackPressed()中执行以下步骤:
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
BottomSheetDialogFragment bottomSheetDialogFragment = (BottomSheetDialogFragment) getSupportFragmentManager().findFragmentByTag("Dashboard"); if (bottomSheetDialogFragment != null) { bottomSheetDialogFragment.dismiss(); }
@Override public void onBackPressed() { InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
BottomSheetDialogFragment bottomSheetDialogFragment = (BottomSheetDialogFragment) getSupportFragmentManager().findFragmentByTag("Dashboard");
if (bottomSheetDialogFragment != null) {
bottomSheetDialogFragment.dismiss();
} else {
super.onBackPressed();
}
}
请注意,在这个样例代码中,我们需要定义一个fragment的tag,以便在需要时进行操作。这里我们使用字符串'Dashboard”作为标签,你也可以更改它以适应你的特定需求。