There is a dialog fragment:

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { // устанавливаем дату, которая отображается в диалоговом окне Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(getActivity().date_choice_millis); int yy = calendar.get(Calendar.YEAR); int mm = calendar.get(Calendar.MONTH); int dd = calendar.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, yy, mm, dd); } public void onDateSet(DatePicker view, int yy, int mm, int dd) { // получаем выбранную в диалоговом окне дату и сохраняем ее Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.DAY_OF_MONTH, dd); calendar.set(Calendar.MONTH, mm); calendar.set(Calendar.YEAR, yy); getActivity().date_choice_millis = calendar.getTimeInMillis(); } } 

And a few Windows (Activity), turning to him. In each Window there is a method that invokes a dialog fragment:

 public void onclick_date_view (View view) { DialogFragment newFragment = new SelectDateFragment(); newFragment.show(getSupportFragmentManager(), "DatePicker"); действия после выбора новой даты ...// Возможно, действия после обновления должны происходить в другом месте? } 

The getActivity (). Date_choice_millis (variable from the calling class) construct does not work. How to get and transfer data from / to the called window?

    1 answer 1

    An example from my project is to transfer from a fragment to the DatePicker and return the result to the fragment. The method correctly transfers the turns of the device and TP. disasters:

    UPD : in connection with the exception of the getCalendarView() method from the Material-style of the calendar rewritten the receipt of the entered date from the parameters passed to the callback

    Date Picker class:

     public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Long mDate = getArguments().getLong("date"); final Calendar c = Calendar.getInstance(); c.setTimeInMillis(mDate); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Calendar с = Calendar.getInstance(); с.set(year,monthOfYear,dayOfMonth); long date = c.getTimeInMillis(); Intent i = new Intent(); i.putExtra ("date" , date); getTargetFragment().onActivityResult(getTargetRequestCode() , Activity.RESULT_OK , i); } } 

    The part of the fragment in which the Date Picker appears by clicking on the TextView (vDate) field, getting the result, as well as displaying the received date in the TextView field that was clicked:

     public class EditTransactionFragment extends Fragment { private final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM); private TextView vDate; private final int CHANGE_DATE = 2; Transaction mTransaction; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); if (mTransaction == null) mTransaction = new Transaction(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { long mDate; View view = inflater.inflate(R.layout.edit_transaction, null); mDate = mTransaction.getDate(); vDate = (TextView) view.findViewById(R.id.tr_date); vDate.setOnClickListener(new DateClickListener(mDate)); setTextDate(mDate); return view; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data == null || resultCode != Activity.RESULT_OK) return; switch (requestCode) { case CHANGE_DATE: Long date = data.getLongExtra("date", 0); mTransaction.setDate(date); setTextDate(date); return; } } private void setTextDate(Long date) { vDate.setText(dateFormat.format(date)); } private class DateClickListener implements View.OnClickListener { private long mDate; public DateClickListener(long date) { mDate = date; } @Override public void onClick(View v) { DialogFragment changeDate = new DatePickerFragment(); Bundle args = new Bundle(1); args.putLong("date", mDate); changeDate.setArguments(args); changeDate.setTargetFragment(EditTransactionFragment.this, CHANGE_DATE); changeDate.show(getFragmentManager(), "changeDate"); } } } 
    • If you need comments on the code, I will add later, I now have a blockage at work: ( - pavlofff
    • Thanks No comments are needed, the idea is clear: there the data is sent to Bundle args, back through Intent i, and onActivityResult. Only the unit in the new Bundle is incomprehensible: Bundle args = new Bundle (1); If you don’t specify anything in parentheses, how will it play a role when accessing DatePickerFragment from different Windows / fragments? - St-St
    • one
      @ St-st Bundle (1) simply means that the size of the bundle is limited to one element — it doesn’t affect anything except that you can put only one argument to save there. You can of course not specify this parameter and use the default constructor Bundle () - pavlofff
    • @pavlofff and is there any advantage to specify the number of parameters for bundles? those. not just the constructor () is constantly used, but (2) or (6) .. - researcher
    • one
      @researcher Here are the same reasons as specifying capacity for collections (an ArrayMap is created in the Bundle to store placed arguments) ArrayMap memory in this case (default capacity = 10 elements) and increasing performance when adding arguments, at a fixed capacity , when a new one is added element does not create a new collection (with more than 10 arguments) - pavlofff