TanakaSakana
TanakaSakana

Reputation: 71

Is it possible to grab listener through fragment?

Currently I start testing not using interface for passing concrete listener from activity to fragments.

this code works:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    //......Skip
    return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener)getActivity(), year, month, day);
}

But when I do it same between fragments, it seem doesn't work.

public Dialog onCreateDialog(Bundle savedInstanceState) {
    //...Skip
    return new TimePickerDialog(getActivity(), ((TimePickerDialog.OnTimeSetListener)((newFragment)getParentFragment())) ,hour, minute,false);
}

I tested with log and the listener from parent fragment seem doesn't called. I have also tried with getContext.

Am I passing wrong listener reference? thanks.

Upvotes: 0

Views: 66

Answers (2)

BoshRa
BoshRa

Reputation: 788

Try this : your fragment must implements TimePickerDialog.OnTimeSetListener , DatePickerDialog.OnDateSetListener

           DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(
                    FragmentName.this, //pass fragment name 
                    Year,
                    Month,
                    Day
            );

Upvotes: 1

Ragesh Ramesh
Ragesh Ramesh

Reputation: 3520

Try with

     public Dialog onCreateDialog(Bundle savedInstanceState) {
//...Skip
return new TimePickerDialog(getActivity(), ((TimePickerDialog.OnTimeSetListener)getParentFragment()) ,hour, minute,false);

}

Make sure parent fragment has implemented TimpePickerDialog.OnTimeSetListener

And yout listener object in TimePickerDialog is initialized with the ParentFragment instance you are using.

Upvotes: 0

Related Questions