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"); } } }