diff --git a/app/src/main/java/com/android/calendar/event/EditEventActivity.java b/app/src/main/java/com/android/calendar/event/EditEventActivity.java index 7acd98a4e1551d22a6408e11e3e74b7d643118f5..9fbadc85bca4318f2e25c1b78684debcb813cd4a 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventActivity.java +++ b/app/src/main/java/com/android/calendar/event/EditEventActivity.java @@ -171,9 +171,19 @@ public class EditEventActivity extends AbstractCalendarActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { - Utils.returnToCalendarHome(this); + onBackPressed(); return true; } return super.onOptionsItemSelected(item); } + + @Override + public void onBackPressed() { + if (mEditFragment != null) { + mEditFragment.onBackPressed(); + return; + } + + super.onBackPressed(); + } } diff --git a/app/src/main/java/com/android/calendar/event/EditEventFragment.java b/app/src/main/java/com/android/calendar/event/EditEventFragment.java index 1b8847bd42a87be4e1f65aee01a65fadc72ec40c..65634597b7b05302a36fa882fb425df8943fdf02 100644 --- a/app/src/main/java/com/android/calendar/event/EditEventFragment.java +++ b/app/src/main/java/com/android/calendar/event/EditEventFragment.java @@ -555,19 +555,37 @@ public class EditEventFragment extends Fragment implements EventHandler, OnColor return mModel.isEmpty(); } - @Override - public void onPause() { + public void onBackPressed() { + if (canSave()) { + showDiscardConfirmAlert(); + return; + } + + Utils.returnToCalendarHome(getActivity()); + } + + private boolean canSave() { Activity act = getActivity(); - if (mSaveOnDetach && act != null && !mIsReadOnly && !act.isChangingConfigurations() - && mView.prepareForSave()) { - mOnDone.setDoneCode(Utils.DONE_SAVE); + return mSaveOnDetach && act != null && !mIsReadOnly && !act.isChangingConfigurations() + && mView.prepareForSave(); + } + + private void showDiscardConfirmAlert() { + new AlertDialog.Builder(getActivity()) + .setMessage(R.string.discard_event_changes) + .setCancelable(true) + .setPositiveButton(R.string.discard, ((dialog, which) -> { + revertEventChanges(); + Utils.returnToCalendarHome(getActivity()); + dialog.cancel(); + })) + .setNegativeButton(R.string.cancel, ((dialog, which) -> dialog.cancel())) + .show(); + } + + private void revertEventChanges() { + mOnDone.setDoneCode(Utils.DONE_REVERT); mOnDone.run(); - } - if (act !=null && (Build.VERSION.SDK_INT < 23 || - ContextCompat.checkSelfPermission(EditEventFragment.this.getActivity(), - Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED)) - act.finish(); - super.onPause(); } @Override diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a978701ba076598d4f3ba147be63f27ee676c315..96ee1a17c5f63c6be4cc87bcc00d9d8e22b9a8f3 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -801,4 +801,7 @@ light + Do you want to discard changes? + Discard + Cancel