如何从DialogFragment / DatePickerFragment将日期传递回Activity?

时间:2012-11-16 08:07:42

标签: java android android-dialog android-dialogfragment android-datepicker

我知道如何用Intent做到这一点,但我该怎么做....

以下是我必须打开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");

一旦打开,如何将我选择的日期传递回Activity

1 个答案:

答案 0 :(得分:1)

您可以创建interface,将其传递给DatePickerFragment(在构造函数中),然后在关闭对话框之前调用method in

例如:

您的界面:

public interface OnDatePicked{
    void onDatePicked();
}

您的活动:

public YourActivity implements OnDatePicked{
    //your Activity
}

你的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();
    }
}