What is the reason for having an android dialog created in a fragment instead of being created directly by an activity?

StackOverflow https://stackoverflow.com/questions/22361566

Вопрос

I'm in the process of learning to develop apps in Android, and I am a bit confused by the docs. I have an activity with a button which is initially filled with a date I got from the Calendar class. When the user clicks on this button, a DatePicker dialog would show up and allow me to set a new date, which should then be written back to that button's text and be used for further processing.

I see the Android Pickers documentation suggests you to create a DatePickerFragment extending DialogFragment, like reproduced here:

public static class DatePickerFragment extends DialogFragment
                            implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // Do something with the date chosen by the user
    }
}

I understood the date set on the dialog will be made available from the onDateSet method, which is the implementation of the DatePickerDialog.OnDateSetListener interface, but what I fail to see is why not just call DatePickerDialog directly from the main activity? (in my case it is just what I ended up doing and it works fine...).

From my novice point of view it seems that implementing a DatePickerFragment is acting like an apparently unnecessary mid layer between Activity and the DatePickerDialog, making it harder than it should be in the sense that for communicating the date set on DatePickerFragment.onDateSet to the main activity would require the interface to be also implemented by the main activity and to communicate the initial date to the fragment I would have to do it through a Bundle object, like shown here.

Is this just a matter of documentation trying to be generalist and start off from a fragment instead of activity (I get the feeling a fragment is a bit like a lightweight activity) or there is really some advantage or thing I'm missing so I don't call the dialog directly from the activity?

Это было полезно?

Решение

Since you're new to Android and haven't yet run into it, let me break it to you: handling orientation changes in Android is a pain. To make it short: the Activity gets recreated every time you change orientation. And all your data members like Dialogs become null.

To combat this there are a couple of ways to handle orientation changes and having a "retained fragment" is one of them. So a DialogFragment is a flavour of this "retained fragment" (don't quote me on this), that doesn't get destroyed on orientation change.

More on this here: http://developer.android.com/guide/topics/resources/runtime-changes.html

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top