DialogFragment callback on orientation change

前端 未结 5 480
执笔经年
执笔经年 2021-02-01 20:40

I\'m migrating my dialogs, currently using Activity.showDialog(DIALOG_ID);, to use the DialogFragment system as discussed in the android reference.

5条回答
  •  佛祖请我去吃肉
    2021-02-01 21:31

    There is better solution instead of using static methods and variables because it would work only fro one instance of your dialog. It is better to store your callback as non static member

    private DialogTestListener mListener;
    public void setListener (DialogTestListener listener){
      mListener = listener;
    }
    

    Then you should show your dialog using TAG like this mDialogFragment.show(getSupportFragmentManager(), DIALOG_TAG);

    And then in onResume method of your activity you can reset your listener

    protected void onResume() {
       super.onResume();
       mDialogFragment = (CMFilterDialogFrg) getSupportFragmentManager().findFragmentByTag(DIALOG_TAG);
       if(mDialogFragment  != null){
           mDialogFragment.setListener(yourListener)
       }
    }
    

提交回复
热议问题