What would be an alternative to the "obsolete" showDialog function?

1

Explanation:

I have a mobile application that uses the showDialog() method at any given time, to show a DatePickerDialog and get the date, but I see that on the following line:

showDialog(DATE_DIALOG_ID);

I have the following alert:

  

The method showDialog (int) from the type Activity is deprecated

It is obsolete, that is, obsolete.

Obsolete functional code:

Declaration of variables:

private int year;
private int month;
private int day;
static final int DATE_DIALOG_ID = 0;

Button btnLembrar = null;

Handler:

View.OnClickListener handler = new View.OnClickListener(){
    public void onClick(View v) {
      if( v.getId()==3 ){
        showDialog(DATE_DIALOG_ID);
      }
    }
};

Assigning handler to button:

btnLembrar.setId(3);
btnLembrar.setOnClickListener(handler);

Override of event onCreateDialog() and listener:

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
       return new DatePickerDialog(this, datePickerListener, year, month,day);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int selectedYear, int selectedMonth, int selectedDay) {
        util xutil=new util();

        year  = selectedYear;
        month = selectedMonth;
        day   = selectedDay;
        String cDta  = xutil.strZero( day, 2 ) + "/" + xutil.strZero( month+1, 2 ) + "/" + xutil.strZero( year, 2 ); 

        btnLembrar.setText(cDta);
    }
};    

Question:

What alternative method of performing this same operation so that it is not obsolete?

    
asked by anonymous 25.03.2014 / 18:07

1 answer

1

In documentation for Android says the following:

  

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.

Where this method should no longer be used and instead use DialogFragment together with FragmentManager .

Note: For older versions you should use the Android compatibility package.

Aki has a good example of how to do it.

There are also some good discussions about these two approaches in SO-EN:

25.03.2014 / 18:29