Alternative to obsolete methods onCreateDialog() and showDialog() from Activity?

Asked

Viewed 95 times

1

I would like to know but the funny thing is that I ran here and it worked even saying that 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);
}

1 answer

1


The indication of obsolete code(deprecated) means that its use should be avoided because it may not be supported in the future and there are currently better/better ways to do so. This does not necessarily mean that the code does not work.

The method onCreateDialog (int id) was considered obsolete in Api level 8 and was then replaced by the onCreateDialog (int id, Bundle args) which subsequently on Api level 13, was also considered obsolete.

Currently the form preferentially used to create dialogs is to use the class Dialogfragment.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.