有没有办法解决BottomSheetDialogFragment的解雇/取消?
底部工作表
public class ContactDetailFragment extends BottomSheetDialogFragment
{
private BottomSheetBehavior.BottomSheetCallback mBottomSheetBehaviorCallback = new BottomSheetBehavior.BottomSheetCallback()
{
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState)
{
if (newState == BottomSheetBehavior.STATE_HIDDEN)
{
dismiss();
}
}
@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset)
{
}
};
@Override
public void setupDialog(Dialog dialog, int style)
{
super.setupDialog(dialog, style);
View contentView = View.inflate(getContext(), R.layout.fragment_contactdetail, null);
dialog.setContentView(contentView);
BottomSheetBehavior mBottomSheetBehavior = BottomSheetBehavior.from(((View) contentView.getParent()));
if (mBottomSheetBehavior != null)
{
mBottomSheetBehavior.setBottomSheetCallback(mBottomSheetBehaviorCallback);
mBottomSheetBehavior.setPeekHeight((int) DisplayUtils.dpToPixels(CONTACT_DETAIL_PEEK_HEIGHT, getResources().getDisplayMetrics()));
}
}
}
我尝试过的不起作用
setupDialog
中添加dialog.setOnCancelListener();
或dialog.setOnDismissListener();
永远不会被触发onStateChanged
,并且没有被解雇/取消的状态ContactDetailFragment.getDialog().setOnCancelListener()
不会被触发鉴于它本质上是一个对话片段,必须有某种方法来解雇?
答案 0 :(得分:8)
找到一个简单的解决方案。
在BottomSheetDialogFragment中使用onDestroy
或onDetach
可以让我正确解雇