8

如何聆听BottomSheetDialogFragment的最终解雇?我想,以节省只有最后的解雇用户改变...BottomSheetDialogFragment - 听取用户事件解散

我尝试以下操作:

方法1

这只火,如果对话框被刷下来驳回(不背衬或触摸外)

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) 
{ 
    Dialog d = super.onCreateDialog(savedInstanceState); 
    d.setOnShowListener(new DialogInterface.OnShowListener() { 
     @Override 
     public void onShow(DialogInterface dialog) { 

      BottomSheetDialog d = (BottomSheetDialog) dialog; 
      FrameLayout bottomSheet = (FrameLayout) dialog.findViewById(android.support.design.R.id.design_bottom_sheet); 

      BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet); 
      behaviour.setState(BottomSheetBehavior.STATE_EXPANDED); 
      behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 
       @Override 
       public void onStateChanged(@NonNull View bottomSheet, int newState) { 
        if (newState == BottomSheetBehavior.STATE_HIDDEN) 
        { 
         // Bottom Sheet was dismissed by user! But this is only fired, if dialog is swiped down! Not if touch outside dismissed the dialog or the back button 
         Toast.makeText(MainApp.get(), "HIDDEN", Toast.LENGTH_SHORT).show(); 
         dismiss(); 
        } 
       } 

       @Override 
       public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

       } 
      }); 
     } 
    }); 
    return d; 
} 

方法2

这并不让我最终解雇,另一种是从屏幕旋转或活动来娱乐区分...

@Override 
public void onDismiss(DialogInterface dialog) 
{ 
    super.onDismiss(dialog); 
    // this works fine but fires one time too often for my use case, it fires on screen rotation as well, although this is a temporarily dismiss only 
    Toast.makeText(MainApp.get(), "DISMISSED", Toast.LENGTH_SHORT).show(); 
} 

问题

我怎么能听,指示事件,用户已完成对话?

回答

9

虽然因此所有类似的问题,建议使用onDismiss我觉得下面是正确的解决方案:

@Override 
public void onCancel(DialogInterface dialog) 
{ 
    super.onCancel(dialog); 
    Toast.makeText(MainApp.get(), "CANCEL", Toast.LENGTH_SHORT).show(); 
} 

这大火,如果:

* the user presses back 
* the user presses outside of the dialog 

这将激活NOT:

* on screen rotation and activity recreation 

解决方案

结合onCancelBottomSheetBehavior.BottomSheetCallback.onStateChanged类似以下内容:

public class Dailog extends BottomSheetDialogFragment 
{ 
    @Override 
    public void onCancel(DialogInterface dialog) 
    { 
     super.onCancel(dialog); 
     handleUserExit(); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    { 
     Dialog d = super.onCreateDialog(savedInstanceState); 
     d.setOnShowListener(new DialogInterface.OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) { 
       BottomSheetDialog d = (BottomSheetDialog) dialog; 
       FrameLayout bottomSheet = (FrameLayout) d.findViewById(android.support.design.R.id.design_bottom_sheet); 
       BottomSheetBehavior behaviour = BottomSheetBehavior.from(bottomSheet); 
       behaviour.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { 
        @Override 
        public void onStateChanged(@NonNull View bottomSheet, int newState) { 
         if (newState == BottomSheetBehavior.STATE_HIDDEN) 
         { 
          handleUserExit(); 
          dismiss(); 
         } 
        } 

        @Override 
        public void onSlide(@NonNull View bottomSheet, float slideOffset) { 

        } 
       }); 
      } 
     }); 
     return d; 
    } 

    private void handleUserExit() 
    { 
     Toast.makeText(MainApp.get(), "TODO - SAVE data or similar", Toast.LENGTH_SHORT).show(); 
    } 
}