I've created TimePicker using this manual http://developer.android.com/guide/topics/ui/controls/pickers.html
public class TimePickerFragment extends DialogFragment {
private TimePickerDialog.OnTimeSetListener mListener;
private Activity mActivity;
private Calendar mCalendar = Calendar.getInstance();
public TimePickerFragment(Activity a,TimePickerDialog.OnTimeSetListener listener) {
mListener = listener;
mActivity = a;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
int hour = mCalendar.get(Calendar.HOUR_OF_DAY);
int minute = mCalendar.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(mActivity, mListener, hour, minute,
DateFormat.is24HourFormat(mActivity));
}
}
I'm trying to show this Dialog with tag this way
public class SomeActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener {
TimePickerFragment mTimePicker;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_view);
mTimePicker = new TimePickerFragment(this, this);
// calling show with "SOME_TAG" tag
mTimePicker.show(this.getSupportFragmentManager(), "SOME_TAG");
}
@Override
public void onTimeSet(TimePicker timePicker, int hourOfDay, int minute) {
// Here tag is null ?!
// Why?
String tag = (String) timePicker.getTag();
}
}
But in the onTimeSet method, tag is always null. Why?
This method is deprecated. androidx. activity.
Showing the DialogFragment Instead, use the show() method to display your dialog. You can pass a reference to a FragmentManager and a String to use as a FragmentTransaction tag.
Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.
tl;dr: The correct way to close a DialogFragment is to use dismiss() directly on the DialogFragment. Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog. Dismiss the fragment and its dialog.
you can simply get tag by getTag() function
public static class DatePickerCustom extends DialogFragment implements
DatePickerDialog.OnDateSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int yy = calendar.get(Calendar.YEAR);
int mm = calendar.get(Calendar.MONTH);
int dd = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, yy, mm, dd);
}
public void onDateSet(DatePicker view, int yy, int mm, int dd) {
String mytag=getTag();
}
}
I know its a late reply but i went through with the same condition recently . So here is my answer ,
the DialogFragment.Show(FragmentManager manager, String tag)
method is set tag for Fragment itself . so this tag can be use as follows :
Fragment dateTo=getActivity().getSupportFragmentManager().findFragmentByTag("dateTo");
if(dateTo!=null){
//do something
}
one use case is you can display more then one fragment in a activity and to identify the fragment you can find that fragment with the given tag.If it is not null, bingo!!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With