Creating your own custom dialogfragment

I came accross an error “java.lang.IllegalStateException: Fragment already added:” whenever I try to show my dialogfragment. FindFragmentByTag is not reliable because I do not like to keep track of the dialog and the surprises that sometime it return NULL.

Here is the example of how one can customize the DialogFragment to your own taste. In this class, I define an extra parameter called isDialogShown to make my life easier so I do not have to rely on getFragmentManager().findFragmentByTag() that most of the time will return null object.

To check if the dialog is up, simple just check mDialogFragment.isDialogShown() and call mDiaglogFragment.dismiss() before


MyCustomDialogFragment dialog = new MyCustomDialogFragment(new OnDialogButtonClickListener(){
        public void onDialogPositiveClick(int status){
          // Do your stuff here

        public void onDialogNegativeClick(int status){
          // Do your stuff here

// Set texts that you want to show in your dialog

Showing dialog:

// Always check if dialog is currently shown
if (dialog.isDialogShown(){
}, MyCustomDialogFragment.TAG);

MyCustomDialogFragment class:

public class MyCustomDialogFragment extends DialogFragment{

    public static String TAG = MyCustomDialogFragment.class.getSimpleName();

    // Define listener that will be called when positive/negative button is pressed.
    public interface OnDialogButtonClickListener{
        public void onDialogPositiveClick(int status);
        public void onDialogNegativeClick(int status);

    private OnDialogButtonClickListener mListener;

    private boolean isDialogShown = false;

    private String mTitleLabel="", mBodyLabel="", mPositiveLabel="", mNegativeLabel="";

    public MyCustomDialogFragment(OnDialogButtonClickListener listener){
        mListener = listener;

     * Method to set dialog parameters
     * @param title for dialog
     * @param content for dialog
     * @param positiveLabel for positive button
     * @param negativeLabel for negative button
    public void setParameters(String title, String content, String positiveLabel, String negativeLabel){
        mTitleLabel = title;
        mBodyLabel = content;
        mPositiveLabel = positiveLabel;
        mNegativeLabel = negativeLabel;

    public void show(FragmentManager manager, String tag) {, tag);
        isDialogShown = true;

    public void dismiss() {
        isDialogShown = false;

    public boolean isDialogShown() {return isDialogShown;}

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            .setPositiveButton(mPositiveLabel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

        // Sometime, we do not want to show negative button
        if (!mNegativeLabel.equals("")){
            builder.setNegativeButton(mNegativeLabel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

        // Create the AlertDialog object and return it
        return builder.create();

Posted on December 20, 2015, in Education and tagged . Bookmark the permalink. 1 Comment.

  1. Thank Colinyeoh, its help me. This specific issues appear in 5.02 but working fine with the kitkat 4.4.4.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: