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

Asked

Viewed 559 times

1

Explanation:

I have a mobile application that uses the method showDialog() at a 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

He’s obsolete, I mean, obsolete.

Obsolete functional code:

Statement 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 the button:

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

Override of the event onCreateDialog() and the Istener:

@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 operation is not obsolete?

1 answer

1

In the documentation 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 in its place should be used DialogFragment along with FragmentManager.

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

Aki has a good example of how to do.

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

  • It’s okay that I can teach you how to do it there on the link if I read everything and understand everything how it works, and maybe quietly implement it in my code, but I don’t have much time to have a chance to read all that and not work in my case. I would like a more objective and self-sustainable response, containing a simple code of a Fragment for example, or even in the best case a Fragment who uses DateTimePicker together (which is my case), but +1 by the links

  • It’s just I answered why I’ve used these references, that you call "links", to implement a solution appropriate to my project, which in my case does not involve DateTimePicker. So I can only help with the references or "links".

Browser other questions tagged

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