2 回答
TA贡献2039条经验 获得超7个赞
在您的片段中使用getActivity().onBackPressed();它将执行onBackPressed()您的父活动
更准确地说
Activity activity = getActivity();
if (activity != null) {
activity.onBackPressed();}
编辑:用于requireActivity()避免空指针
requireActivity().onBackPressed();
TA贡献1884条经验 获得超4个赞
onBackPressed 回调由您的父活动处理。因此您可以覆盖父活动的回调
XyzActivity.java:
@Override
public void onBackPressed() {
handleBackPress();
}
public void handleBackPress() {
Fragment visibleFragment = getSupportFragmentManager().findFragmentById(R.id.fragmentFrameLayout);
if (visibleFragment == null) {
return;
}else if (visibleFragment instanceof PreviewFragment) {
CommonFunctions.showDialogActionable(this, "Confirm", "Are you sure you want to exit?",
"Yes", (dialogInterface, i) -> {
finish();
}, "No", null, "", null, true);
return;
}else
finish();
super.onBackPressed();
}
添加回答
举报