DialogFragment callback on orientation change

前端 未结 5 462
执笔经年
执笔经年 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:36

    While André's solution works, a better solution is to get the updated activity during onAttach() in your Fragment.

    private DialogTestListener mListener;
    
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mListener = (DialogTestListener) activity;
    }
    

    With this solution, you won't need to pass the Activity in newInstance() anymore. You just need to make sure the Activity owning your Fragment is a DialogTestListener. You also don't need to save the state like in the ResultReceiver solution.

提交回复
热议问题