Send Data from DialogFragment to Fragment

前端 未结 3 1602
花落未央
花落未央 2021-01-22 08:06

I have a fragment which contain an EditText when I click on that EditText a DatePicker Dialog appears to select date.
FragmentFile

public c         


        
相关标签:
3条回答
  • 2021-01-22 08:56

    I have sloved the problem thank's to this topic, the seconde response of topic.
    DatePickerFragment.java

    @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            Calendar c = Calendar.getInstance();
            c.set(year, month, day);
    
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            String formattedDate = sdf.format(c.getTime());
            // in this part I stored the selected date into the intent
            Intent i = new Intent();
            i.putExtra("selectedDate",formattedDate);
            getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, i);
        }
    

    Fragment File

    public static final int DATEPICKER_FRAGMENT=1; // adding this line
    @Override
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_feeding, container, false);
        EditText editText = (EditText) view.findViewById(R.id.foodDeliveryDateFiled);
    
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DialogFragment picker = new DatePickerFragment();
                picker.setTargetFragment(FeedingFragment.this, DATEPICKER_FRAGMENT);
                picker.show(getFragmentManager().beginTransaction(), "Date Picker");
            }
        });
        return view;
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode) {
            case DATEPICKER_FRAGMENT:
                if (resultCode == Activity.RESULT_OK) {
                    // here the part where I get my selected date from the saved variable in the intent and the displaying it.
                    Bundle bundle=data.getExtras();
                    String resultDate = bundle.getString("selectedDate","error");
                    EditText editText = (EditText) getView().findViewById(R.id.foodDeliveryDateFiled);
                    editText.setText(resultDate);
                }
                break;
        }
    
    0 讨论(0)
  • 2021-01-22 08:57

    Every Java programmer's first response would be to create an observable or callback and send it around. While this would be an acceptable practice in most places, when working with Fragments and DialogFragments the experience is not as pretty, due to state losses when recreating, rotating, pausing...you're bound to receive IllegalStateExceptions you're not expecting.

    My advice, while a bit drastic, would be to move your communication patterns from a family of interface callbacks being passed around to a centralized event bus like Otto or GreenRobot's. It works just great both in foreground and background for any given state and helps decoupling usage.

    0 讨论(0)
  • 2021-01-22 09:07

    Updated code


    FragmentFile

    public class FirstFragment extends Fragment implements DatePickerDialogFragmentEvents{
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_first, container, false);
            EditText editText = (EditText) view.findViewById(R.id.visitDateFiled);
    
            editText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    DialogFragment picker = new DatePickerFragment();
                    picker.setDatePickerDialogFragmentEvents(FirstFragment.this); //Changed
                    picker.show(getFragmentManager(), "Date Picker");
                }
            });
            return view;
        }
    
        //This is where you will get selected date value
        @Override
        public void onDateSelected(String date) {
            String selectedDate = date;
        } 
     }
    

    DialogFragment

    public class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {
    
        //Interface created for communicating this dialog fragment events to called fragment
        public interface DatePickerDialogFragmentEvents{
           void onDateSelected(String date);
        }
    
        DatePickerDialogFragmentEvents dpdfe
    
        public void setDatePickerDialogFragmentEvents(DatePickerDialogFragmentEvents dpdfe){
             this.dpdfe = dpdfe;
        }
    
        @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);
        }
    
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            Calendar c = Calendar.getInstance();
            c.set(year, month, day);
    
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            String formattedDate = sdf.format(c.getTime());
            dpdfe.onDateSelected(formattedDate); //Changed
        }
    }
    

    So i created an interface named DatePickerDialogFragmentEvents in your dialogfragment class and implemented that in caller fragment. From caller fragment when you create a instance of dialogfragment, set DatePickerDialogFragmentEvents instance of fragment by using setDatePickerDialogFragmentEvents method of dialog fragment class.

    0 讨论(0)
提交回复
热议问题