diff --git a/res/values/strings.xml b/res/values/strings.xml
index b2c2e583c472faf7ca390ed0f930251678e8e960..e6aabdc3ed69c6525b0631a9e87924ce11256e7a 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -783,4 +783,7 @@
Offline Calendar
+ Do you want to discard changes?
+ Discard
+ Cancel
diff --git a/src/com/android/calendar/event/EditEventActivity.java b/src/com/android/calendar/event/EditEventActivity.java
index ef3d1ee2badcfae9fb6d7f75a3e5ed5f76ca7230..077aa961f3dcbefc406924f91c25c902bf63fdd1 100644
--- a/src/com/android/calendar/event/EditEventActivity.java
+++ b/src/com/android/calendar/event/EditEventActivity.java
@@ -170,9 +170,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/src/com/android/calendar/event/EditEventFragment.java b/src/com/android/calendar/event/EditEventFragment.java
index b512c6235c351793bd136e24e23df476494ee23c..5635c6473fed69fdd7b94cfc022a564a121cb578 100644
--- a/src/com/android/calendar/event/EditEventFragment.java
+++ b/src/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