简体   繁体   中英

How do I pass back a date to an Activity from a DialogFragment / DatePickerFragment?

I know how to do this with Intent, but how do I do this....

Here is the code I have to open the DatePickerFragment :

DialogFragment newFragment = new DatePickerFragment();

Bundle bundle = new Bundle();
bundle.putString("date", (String) itemDict.get(Helper.convertKeyString(name, false)));
newFragment.setArguments(bundle);

newFragment.show(getFragmentManager(), "datePicker");

Once that is open, how do I pass the date I choose back to my Activity ?

You can create interface , pass it to DatePickerFragment (in constructor), and then call method in before closing the dialog

For example:

Your interface:

public interface OnDatePicked{
    void onDatePicked();
}

Your Activity:

public YourActivity implements OnDatePicked{
    //your Activity
}

And your DatePickerFragment:

public class DatePickerFragment { //change Object to required type
    private OnDatePicked listener;

    public DatePickerFragment(OnDatePickedlistener){
        this.listener=listener;
    }

    //required methods
    protected void method(Object o){
        //your stuff
        listener.onDatePicked();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM