Search code examples
androidandroid-fragmentsinstantiationandroid-dialogfragment

Forcing the developer to place a non-empty tag in the DialogFragment show method?


I want to enforce the use of having a tag used in the show method for a DialogFragment:

MyDialogFragment fragment = MyDialogFragment.newInstance(args);
fragment.show(getSupportFragmentManager(), MANDATORY_TAG);

Is there a way to enforce this, or throw an error from within the fragment somehow, if MANDATORY_TAG is "" or null?


Solution

  • if (tag == null || tag.isEmpty()) {
                throw new NullPointerException("Please add the Tag. Tag cannot be null or empty");
            }
    

    Edit:

    try {
        MyDialogFragment fragment = MyDialogFragment.newInstance(args);
        fragment.show(getSupportFragmentManager(), MANDATORY_TAG);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }