Android : Android DialogFragment - need to call from Activity and Fragment

on Thursday, September 25, 2014


I am implementing a date picker that extends DialogFragment. I need to be able to instantiate the class from within an Activity (which currently works fine) and from within a Fragment (which does not).



public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

TheListener listener;

public interface TheListener {
public void returnDate(View v, String date);
}

@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);
listener = (TheListener) getActivity();

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {

Calendar c = Calendar.getInstance();
c.set(year, month, day);
Log.i(" DatePickerFragment", " in set date");

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) {
listener.returnDate(view, formattedDate);
}
}


}


When I try to instantiate the date picker from within a Fragment, I get the following run-time error:



FATAL EXCEPTION: main
Process: com.example.app.debug, PID: 16656
java.lang.ClassCastException: com.example.app.UI.MainActivity cannot be cast to com.example.app.UI.DatePickerFragment$TheListener
at com.example.app.UI.DatePickerFragment.onCreateDialog(DatePickerFragment.java:36)
at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
at android.app.BackStackRecord.run(BackStackRecord.java:684)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)


I know this error is due to the following line of code from the onCreateDialog method in the date picker class, as the MainActivity knows nothing about the date picker.



listener = (TheListener) getActivity();


The call getActivity() returns the "parent activity" of the fragment - which is MainActivity. This doesn't implement the Listener interface. All fragments in the app are instantiated by the MainActivity. The date picker is successfully instantiated using the current code by a separate activity which does implement the Listener interface.


What I need to know is, how can I use this in both an Activity and a Fragment? I am guessing that I could instantiate the date picker from within the Main Activity, but I also might need it inside other Fragments so this way could get messy. Another way is to fix the listener so that is can be fired from inside a Fragment, but I am not sure how. Any suggestions as to how best to solve this problem?


0 comments:

Post a Comment