Code example for DialogFragment

Methods: getFragmentManager

1
import android.support.v4.app.DialogFragment; 
import android.text.format.DateFormat; 
import android.view.View; 
import android.widget.TimePicker; 
 
public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {
	private TimePickedListener mListener;
 
	@Override 
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final Calendar c = Calendar.getInstance();
		int hour = c.get(Calendar.HOUR_OF_DAY);
		int minute = c.get(Calendar.MINUTE);
 
		return new TimePickerDialog(getActivity(), this, hour,minute,DateFormat.is24HourFormat(getActivity()));
	} 
 
	public interface TimePickedListener { 
		public void onTimePicked(int hour, int minute);
	}		 
 
	@Override 
	public void onAttach(Activity activity) {
		super.onAttach(activity);
 
		// This makes sure that the container activity has implemented 
		// the callback interface. If not, it throws an exception 
		try { 
			mListener = (TimePickedListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnTimeSetListener"); 
		} 
	} 
 
 
	public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
		mListener.onTimePicked(hourOfDay, minute);
	} 
 
	public void showTimePickerDialog(View view) {
		DialogFragment newFragment = new TimePickerFragment ();
		newFragment.show(getFragmentManager(), "timePicker");
	} 
 
}