Can’t override onCreateDialog method of androidx.fragment.app.Dialog

When I try to override the onCreateDialog method of androidx.fragment.app.DialogFragment I get the following error: ‘onCreateDialog overrides nothing’.

According to developer.android.com I should be able to override it as it’s defined as open.

I am using the following code

import android.app.AlertDialog
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import androidx.fragment.app.DialogFragment


class MyDialogFragment : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage("Yes or no?")
                .setPositiveButton("yes",
                    DialogInterface.OnClickListener { dialog, id ->
                        // yes
                    })
                .setNegativeButton("no",
                    DialogInterface.OnClickListener { dialog, id ->
                        // no
                    })
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

What’s going on here?

Answer

If you look closely at the method signature you will see that the parameter is marked with the @Nullable, and its type is Bundle?.

onCreateDialog(@Nullable savedInstanceState: Bundle?)

But in your case you have defined the type of parameter as Bundle which is non nullable, making your function signature different than the one you are trying to override hence the error