Alternative to the obsolete onCreateDialog () and showDialog () methods of Activity?

1

I would like to know but the funny thing is that I performed here and it worked fine even though it is obsolete. What should I do?

 private Button botao;

static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_date_pick);
    botao = (Button) findViewById(R.id.button);
    botao.setOnClickListener(this);
}

@Override
protected Dialog onCreateDialog(int id) {
    Calendar calendario = Calendar.getInstance();

    int ano = calendario.get(Calendar.YEAR);
    int mes = calendario.get(Calendar.MONTH);
    int dia = calendario.get(Calendar.DAY_OF_MONTH);

    switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, ano, mes,
                    dia);
    }
    return null;
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        String data = String.valueOf(dayOfMonth) + " /"
                + String.valueOf(monthOfYear+1) + " /" + String.valueOf(year);
        Toast.makeText(getApplicationContext(),
                "DATA = " + data, Toast.LENGTH_SHORT)
                .show();
    }
};

@Override
public void onClick(View v) {
    if (v == botao)
        showDialog(DATE_DIALOG_ID);
}
    
asked by anonymous 19.12.2016 / 00:57

1 answer

1

Deprecated code means that its use should be avoided because it may not be supported in the future and there are currently better / preferable ways of doing so. This does not necessarily mean that the code does not work.

The onCreateDialog (int id) method was considered obsolete in < in> Api level 8 and was replaced at the time by > onCreateDialog (int id, Bundle args) , which was later also considered obsolete in Api level 13 .

Currently the preferred way to create dialogs is to use the DialogFragment .

    
19.12.2016 / 15:43