Android 4.1.2 dialogs are called twice

I have this problem with my application, when I call to show a dialog, it was called twice. This only happens with Android 4.1 and higher. The lower version is working fine, so I don't think this is a problem with the code.

Have you heard this problem before?

here is the code:

Button edit = (Button) ad.findViewById(R.id.editBtn); edit.setTypeface(roboto); edit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setDate(); ad.dismiss(); } }); ad.show(); ad.setOnDismissListener(new OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { shiftsActivity.setPressed(true); } }); } public void setDate() { // Initialize and open the set date dialog DatePickerDialog setDateDialog = new DatePickerDialog(Shifts.this, datePickerListener, dateAndTime.get(Calendar.YEAR), dateAndTime.get(Calendar.MONTH), dateAndTime.get(Calendar.DAY_OF_MONTH)); setDateDialog.setTitle("Set Date"); setDateDialog.show(); } public void setStartTime() { TimePickerDialog setStartTimeDialog = new TimePickerDialog(Shifts.this, startTimePicker, dateAndTime.get(Calendar.HOUR), dateAndTime.get(Calendar.MINUTE), true); setStartTimeDialog.setTitle("Started At:"); setStartTimeDialog.show(); } public void setEndTime() { TimePickerDialog setEndTimeDialog = new TimePickerDialog(Shifts.this, setEndTime, dateAndTime.get(Calendar.HOUR), dateAndTime.get(Calendar.MINUTE), true); setEndTimeDialog.setTitle("Ended:"); setEndTimeDialog.show(); } TimePickerDialog.OnTimeSetListener startTimePicker = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { startIntHours = hourOfDay; startIntMinutes = minute; editStartTime = String.format("%02d", hourOfDay) + ":" + String.format("%02d", minute); setEndTime(); } }; TimePickerDialog.OnTimeSetListener setEndTime = new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { finishIntHours = hourOfDay; finsihIntMinutes = minute; if (finishIntHours < startIntHours) { finishIntHours = finishIntHours + Utility.HOURS_TIME_UNIT; } if (finsihIntMinutes < startIntMinutes) { finsihIntMinutes = finsihIntMinutes + Utility.MINUTES_TIME_UNIT; } totalHours = finishIntHours - startIntHours; totalMinutes = finsihIntMinutes - startIntMinutes; Log.i("TotalHours in time picker", "" + totalHours); Log.i("Totalminute in time picker", "" + totalMinutes); editEndTime = String.format("%02d", hourOfDay) + ":" + String.format("%02d", minute); replace(Shifts.view, Shifts.position); } }; 
+13
android dialog
Dec 6 '12 at 17:18
source share
8 answers

According to your code, none of these methods are ever called because you never use TimePickerDialogs .

However, there is a known issue with the DatePickerDialog / TimePickerDialog , which may be relevant: http://code.google.com/p/android/issues/detail?id=34833

+12
Dec 11 '12 at 12:17
source share

Here's a basic solution for those who need a TimeChooserDialog that doesn't call the listener twice, with basic options

 public static AlertDialog getTimePickerDialog(Context context, final OnTimeSetListener listener, int hour, int minute, boolean is24HFormat) { AlertDialog.Builder builder = new AlertDialog.Builder(context); final TimePicker timePicker = new TimePicker(context); timePicker.setIs24HourView(is24HFormat); timePicker.setCurrentHour(hour); timePicker.setCurrentMinute(minute); builder.setView(timePicker); builder.setPositiveButton(android.R.string.ok, new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { if(null != listener) { listener.onTimeSet(timePicker, timePicker.getCurrentHour(), timePicker.getCurrentMinute()); } } }); builder.setNegativeButton(android.R.string.cancel, null); return builder.create(); } 
+3
Aug 6 '13 at 14:36
source share

If you look at the source of the android, it will appear that onTimeSet () is called during onStop (). The dialog also calls onTimeSet () in it the onClick () method.

To get around this, I had to redefine the dismissal dialogs and cancel the functions to set a boolean, which I checked if onTimeSet was called using the click or cancel / cancel button

+2
Jul 26 '13 at 13:24
source share

The same thing happens to me for TimePicker. I solve this issue.

 new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int setHour, int setMinute) { if (view.isShown()) { // This method will return true only once } } }; 

iShown ():

Returns the visibility of this view and all its ancestors

Returns True if this look and all of its ancestors are visible

+1
Nov 14 '14 at 9:53
source share

I try and it works well with me, if you still have a problem, try using classic singleton

Something like this :) more about this.

 public void setStartTime() { TimePickerDialog setStartTimeDialog = NULL; if(setStartTimeDialog == null) { new TimePickerDialog(Shifts.this, startTimePicker, dateAndTime.get(Calendar.HOUR),dateAndTime.get(Calendar.MINUTE), true); setStartTimeDialog.setTitle("Started At:"); } setStartTimeDialog.show(); } 
0
Dec 10
source share

Do not use the built-in TimePickerDialog or DatePickerDialog. Instead, create a custom dialog, as shown here. It is simple and convenient to use and just works!

  AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); final TimePicker timePicker = new TimePicker(getActivity()); builder.setTitle("Set Time"); builder.setView(timePicker); builder.setNegativeButton("Cancel", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.setPositiveButton("OK"), new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { int minute = timePicker.getCurrentMinute(); int hourOfDay = timePicker.getCurrentHour(); } }); builder.show(); 
0
Sep 04 '14 at 18:35
source share

Instead of breaking super.onClose () or creating custom dialogs, I solve it by wrapping the real listener and expanding the TimePickerDialog, which enables and disables forwarding from the wrapper to the real listener. This is not a drop in the replacement, but it is neat enough.

 public class TimePickerDialogCustom extends TimePickerDialog { protected TimePickerDialogOnTimeSetListenerWrapper listenerWrapper; public TimePickerDialogCustom(Context context, TimePickerDialogOnTimeSetListenerWrapper listener, int hourOfDay, int minute, boolean is24HourView) { super(context, listener, hourOfDay, minute, is24HourView); listenerWrapper = listener; } @Override protected void onStop() { if (listenerWrapper != null) listenerWrapper.enabled = false; super.onStop(); if (listenerWrapper != null) listenerWrapper.enabled = true; } public static final class TimePickerDialogOnTimeSetListenerWrapper implements TimePickerDialog.OnTimeSetListener { private boolean enabled = true; private final TimePickerDialog.OnTimeSetListener delegate; public TimePickerDialogOnTimeSetListenerWrapper(TimePickerDialog.OnTimeSetListener delegate) { this.delegate = delegate; } @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { if (enabled) { this.delegate.onTimeSet(view, hourOfDay, minute); } } } } 

Then, to use it:

  TimePickerDialogCustom.TimePickerDialogOnTimeSetListenerWrapper listenerWrapper = new TimePickerDialogCustom.TimePickerDialogOnTimeSetListenerWrapper(realListener); // Create a new instance of DatePickerDialog and return it TimePickerDialogCustom dialog = new TimePickerDialogCustom( getActivity(), listenerWrapper, hourOfDay, minute, DateFormat.is24HourFormat(getActivity())); 
0
Jan 30 '15 at 9:08
source share

I ran into this problem showing twice. I exceeded onStop () TimePickerDialog and solved this problem. In addition, you should not call super.onStop () in your overriden onStop ().

 @Override public void onStop() { //super.onStop(); Log.d(TAG, "onStop of starttime TimePickerDialog."); } 
-one
Aug 07 '13 at 10:20
source share



All Articles