DialogFragment and onDismiss

前端 未结 8 1837
迷失自我
迷失自我 2020-11-27 16:14

I am using a DialogFragment, which I am showing like this from an Activity:

DialogFragmentImage dialog = DialogFragmentImage.newIns         


        
相关标签:
8条回答
  • 2020-11-27 16:42

    This is an old issue but I found no solution I am happy with. I don't like passing any Listeners to my DialogFragment or set a TargetFragment, because that may break on orientation change. What do you think about this?

            MyDialog d = new MyDialog();
            d.show(fragmentManager, "tag");
            fragmentManager.registerFragmentLifecycleCallbacks(new FragmentManager.FragmentLifecycleCallbacks() {
                @Override
                public void onFragmentViewDestroyed(FragmentManager fm, Fragment f) {
                    super.onFragmentViewDestroyed(fm, f);
                    //do sth      
            fragmentManager.unregisterFragmentLifecycleCallbacks(this);
                    }
                }, false);
    
    0 讨论(0)
  • 2020-11-27 16:43

    Alternative answer, if you don't have access to the methode onDismiss of activity.

    //DIALOGFRAGMENT
    //Create interface in your DialogFragment (or a new file)
    public interface OnDismissListener {
       void onDismiss(MyDialogFragment myDialogFragment);
    }
    //create Pointer and setter to it
    private OnDismissListener onDismissListener;
    public void setDissmissListener(DissmissListener dissmissListener) {
       this.dissmissListener = dissmissListener;
    }
    //Call it on the dialogFragment onDissmiss
    @Override
    public void onDismiss(DialogInterface dialog) {
       super.onDismiss(dialog);
    
       if (onDismissListener != null) {
          onDismissListener.onDismiss(this);
       }
    }
    
    //OTHER CLASS, start fragment where you want
    MyDialogFragment df = new MyDialogFragment();
    df.setOnDismissListener(new MyDialogFragment.OnDismissListener() {
       @Override
       public void onDismiss(MyDialogFragment myDialogFragment) {
          //Call when MyDialogFragment close
       }
    });
    df.show(activity.getFragmentManager(), "myDialogFragment");
    

    edit : if system need to recreate DialogFragment: you can find it with

    MyDialogFragment myDialogFragment = getFragmentManager().findFragmentByTag("MyDialogFragment"); 
    if(myDialogFragment != null) { 
       myDialogFragment.setOnDismissListener(...); 
    }
    
    0 讨论(0)
提交回复
热议问题