Refresh fragment when dialogfragment is dismissed

前端 未结 6 1859
南方客
南方客 2021-02-04 01:01

Is there any way I can detect when a DialogFragment is dismissed, so that i can update its parent fragment?

6条回答
  •  长发绾君心
    2021-02-04 01:39

    I tried @Gazer answer, it doesn't work for me, I am using different fragment class

    import android.app.Fragment; 
    

    But I got this working

    1. Create an interface class

      public interface MyDialogListener {
          void OnCloseDialog(Object obj); //you can put any object here
      }
      
    2. Implement the interface class in the Parent Fragment

      public class ActionBarFragment extends Fragment implements MyDialogListener{
      
          @Override
          public void OnCloseDialog(Object obj) {
              //Do you refresh
          }
      
    3. and then I add the listener inside the DialogFragment

      public class SpecialDialogFragment extends DialogFragment { 
      
          MyDialogListener mListener;
      
          public SpecialDialogFragment(MyDialogListener listener) {
              this.mListener = listener;
          }
      
          @Override
          public void onStop() {
              super.onStop();
              if(mListener!=null)
                 mListener.OnCloseDialog(null);
          }
      

提交回复
热议问题