Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.1.2 dialogs are called twice

Tags:

android

dialog

I have this issue with my app when I call to show a dialog, it is being called twice somehow. This only happens with android 4.1 and above. Lower version works fine so I dont think it's any code issue.

Did you heard\encountered this issue before?

here 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);

    }
};
like image 814
Yosi199 Avatar asked Dec 06 '12 17:12

Yosi199


People also ask

What are the dialog classes in Android?

There are three kinds of lists available with the AlertDialog APIs: A traditional single-choice list. A persistent single-choice list (radio buttons) A persistent multiple-choice list (checkboxes)

What is implement dialog in Android?

Android Alert Dialog is built with the use of three fields: Title, Message area, Action Button. Alert Dialog code has three methods: setTitle() method for displaying the Alert Dialog box Title. setMessage() method for displaying the message.

How do I know if my android has dialog?

Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().

What is a dialogue fragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 Answers

According to your code, neither of those methods are ever called, because you never use the TimePickerDialogs.

That being said, there is a known issue related to the behavior of DatePickerDialog/TimePickerDialog that may be relevant: http://code.google.com/p/android/issues/detail?id=34833

like image 111
CommonsWare Avatar answered Sep 20 '22 14:09

CommonsWare


Here is a basic solution, for someone who needs a TimeChooserDialog, that does not 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();
}
like image 21
andre Avatar answered Sep 18 '22 14:09

andre