Java – “Overrides deprecated method” on onCreateDialog

“Overrides deprecated method” on onCreateDialog… here is a solution to the problem.

“Overrides deprecated method” on onCreateDialog

I

have a class in which I tried to create a Dialog, but when I put the method onCreateDialog, it looks like it’s crossed by a line. It gives me the following error message:

Overrides deprecated method in ‘android.app.Activity’.

This inspection reports where deprecated method is used in the specified inspection scope.

But when I look at the official documentation for Android, I don’t see that this method has been deprecated, so I don’t understand what happens: onCreateDialog

My onCreateDialog code:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case 1:
            return new DatePickerDialog(this, datePickerListener, year, month, day);
        case 2:
            return new TimePickerDialog(this, timePickerListener, hour, minute,false);
    }

return null;
}

But if I execute my app, it works fine, so I have some problems :

  • Why does it let me execute the application?

  • How do I fix it?

  • Is it bad for this message to appear? I mean, could this be an issue related to it? Does it create future problems?

Any help that helps me clarify would be appreciated.

Thanks in advance!

Solution

Activity.onCreateDialog () is deprecated.

This method was deprecated in API level 13.
Use the new DialogFragment class with FragmentManager instead; this is also available on older platforms through the Android compatibility package.

Deprecated code means that its use is discouraged and may not be supported in the future. This does not necessarily mean that the code does not work. It may still run as before, but if a change is made that breaks this feature, there is no guarantee that anyone will fix it because they have stated that it should not be used.

To “fix” it, you should look at DialogFragment and how to use fragments in general, and display one of the fragments.

Related Problems and Solutions