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

SO에 대한 모든 유사한 질문이 올바른 해결책 나는 다음과 같은 생각 onDismiss이다 사용하는 것이 좋습니다 있지만 :

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

이되지 발사 : 다음과 같은 경우

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

이 불을

* on screen rotation and activity recreation 

솔루션

결합 으로 onCancel

는 다음과 같이을 BottomSheetBehavior.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(); 
    } 
}