0

I am follow this library for datetime picker https://github.com/florent37/SingleDateAndTimePicker its working fine but if i am open the SingleDateAndTimePicker dialog and fire onBack press method then dialog box is close but am unable to go back privous actvity and if dialog box is not open then onBack press code is fire any solution on that would be appreciated. my code:

 @Override
public void onBackPressed() {
    long currentTime = System.currentTimeMillis();
    if (singleDateAndTimePickerDialog!=null) {
        singleDateAndTimePickerDialog.dismiss();

    } else if (currentTime - lastPress > 5000) {
        Toast.makeText(getBaseContext(), "Press back again", Toast.LENGTH_LONG).show();
        lastPress = currentTime;
    } else {
        super.onBackPressed();
    }
}
ashish
  • 307
  • 4
  • 15
  • 1
    might be duplicate of [onbackpress event not firing when dialog is open](https://stackoverflow.com/questions/17655239/onbackpress-not-called-when-pop-up-window-is-displayed) – Flutterian Nov 09 '17 at 06:39
  • yamini if i am using finish() then activity also got closed. – ashish Nov 09 '17 at 06:56

4 Answers4

1

You can try this:

// a global variable private SingleDateAndTimePickerDialog singleDateAndTimePickerDialog = null;

        onCreate(){ 
        singleDateAndTimePickerDialog = new SingleDateAndTimePickerDialog(this);
                   }

                   @Override
                   public void onBackPressed() {

        if(singleDateAndTimePickerDialog != null 
        && singleDateAndTimePickerDialog.isDisplaying()){

                     singleDateAndTimePickerDialog.dismiss();
                     return;
                    }//if

                      super.onBackPressed();
                      finish();

                    }

==================================EDIT=====================================

You can use android DatePicker and TimePicker.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:padding="8dp"
    android:layout_height="match_parent">

    <DatePicker
        android:id="@+id/date_picker"
        android:layout_width="match_parent"
        android:calendarViewShown="true"
        android:spinnersShown="false"
        android:layout_weight="4"
        android:layout_height="0dp" />

    <TimePicker
        android:id="@+id/time_picker"
        android:layout_weight="4"
        android:layout_width="match_parent"
        android:layout_height="0dp" />

    <Button
        android:id="@+id/date_time_set"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:text="Set"
        android:layout_height="0dp" />

</LinearLayout>

..and android dialog.

final View dialogView = View.inflate(activity, R.layout.date_time_picker, null);
final AlertDialog alertDialog = new AlertDialog.Builder(activity).create();

dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

         DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
         TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);

         Calendar calendar = new GregorianCalendar(datePicker.getYear(),
                            datePicker.getMonth(),
                            datePicker.getDayOfMonth(),
                            timePicker.getCurrentHour(),
                            timePicker.getCurrentMinute());

         time = calendar.getTimeInMillis();
         alertDialog.dismiss();
    }});
alertDialog.setView(dialogView);
alertDialog.show();

So you can do it like this:

   @Override
   public void onBackPressed() {

   if(alertDialog != null  && alertDialog.isShowing()){

   alertDialog.dismiss();
   return;
   }//if

   super.onBackPressed();
   finish();

   } 
propoLis
  • 1,229
  • 1
  • 14
  • 48
0

You can try finishing the activity, once the dialog got dismissed.

 @Override public void onBackPressed() {
long currentTime = System.currentTimeMillis();
if (singleDateAndTimePickerDialog!=null) {
    singleDateAndTimePickerDialog.dismiss();
    finish(); // Or super.onBackPressed();
} else if (currentTime - lastPress > 5000) {
    Toast.makeText(getBaseContext(), "Press back again", Toast.LENGTH_LONG).show();
    lastPress = currentTime;
} else {
    super.onBackPressed();
} 
}
Yamini Balakrishnan
  • 2,361
  • 16
  • 24
0

Try this one

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
    }
        return super.onKeyDown(keyCode, event);
    }
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
mehul chauhan
  • 1,792
  • 11
  • 26
  • mehul its closing activity also my code is working fine but its not fire if dialog box is open – ashish Nov 09 '17 at 07:09
0

super.onBackPressed();

in else condition it's ok with your condition because in this condition onback not fire because super.onback in not proper condition.