Question

If I have an ArrayList<DialogFragment> containing DialogFragments of unknown size, how can I programmatically cue up each one so that once the first one is dismissed, the next one is shown, and so on?

for (int i = 0; i < tutorialViews.size(); i++) {
    final int current = i;
    DialogFragment someDialogFragment = dialogFragmentList.get(i);

    if (i == 0) {
        someDialogFragment .show(activity.get().getSupportFragmentManager(), "some_dialog_fragment");
    } 

    if (i + 1 != dialogFragmentList.size() - 1) {
        someDialogFragment.getDialog().setOnCancelListener(new OnCancelListener() {
            @Override
            public void onCancel(DialogInterface arg0) {
                dialogFragmentList.get(current + 1).show(activity.get().getSupportFragmentManager(), "more_dialog_fragments");
            }
        });
    }
}

Unforunately this doesn't work since the dialog object within a dialogFragment isn't instantiated yet, giving a nullPointerException for the getDialog() call

Was it helpful?

Solution

Create your own interface to callback when the fragmentdialog is closed.

OnDialogFragHide mListener;

public interface OnDialogFragHide {
    public void onFragmentDismissed();
}

public void setOnFragDismissedListener(OnDialogFragHide listener) {
    mListener = listener;
}

Register the interface in the for loop

if (i == 0) {
    tutorial.show(activity.get().getSupportFragmentManager(), "smoking_hawt");
} 

if (i != tutorialViews.size() - 1) {
    tutorial.setOnFragDismissedListener(new OnDialogFragHide() {
        @Override
        public void onFragmentDismissed() {
            tutorialViews.get(current + 1).show(activity.get().getSupportFragmentManager(), "some_tag");
        }
    });
}

Call upon the listener whenever the fragment is closed (i.e. in the FragmentDialog's onDismiss() and onCancel() methods, NOT the DIALOG's onDismiss / onCancel listeners.

@Override
public void onDismiss(DialogInterface dialog) {
    if (mListener != null && !dismissed) {
        dismissed = true;
        mListener.onFragmentDismissed();
    } else {
        Log.e(TAG, "DialogFragmentDismissed not set");
    }
    super.onDismiss(dialog);
}

@Override
public void onCancel(DialogInterface dialog) {
    if (mListener != null && dismissed) {
        dismissed = true;
        mListener.onFragmentDismissed();
    } else {
        Log.e(TAG, "DialogFragmentDismissed not set");
    }
    super.onCancel(dialog);
}

the dismissed boolean is for good measure to make the listener isn't called twice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top