From ff8d15c564e26462350c15c13678fee6423ec864 Mon Sep 17 00:00:00 2001 From: Prab Date: Tue, 1 May 2018 17:37:53 +1000 Subject: [PATCH] Cancel the Pending Intent Cancel the Pending Intent when you cancel the alarm. Not doing this can cause the toggle button to show up in the "ON" position even when the alarm is cancelled. --- .../main/java/com/example/android/standup/MainActivity.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/StandUp/app/src/main/java/com/example/android/standup/MainActivity.java b/StandUp/app/src/main/java/com/example/android/standup/MainActivity.java index 2f4d809..b2cf4c5 100644 --- a/StandUp/app/src/main/java/com/example/android/standup/MainActivity.java +++ b/StandUp/app/src/main/java/com/example/android/standup/MainActivity.java @@ -50,6 +50,7 @@ protected void onCreate(Bundle savedInstanceState) { boolean alarmUp = (PendingIntent.getBroadcast(this, 0, notifyIntent, PendingIntent.FLAG_NO_CREATE) != null); + //only depends on the Pending Intent so remember to manage the lifecycle of the Pending Intent properly alarmToggle.setChecked(alarmUp); //Set up the PendingIntent for the AlarmManager @@ -79,6 +80,10 @@ public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { //Cancel the alarm and notification if the alarm is turned off alarmManager.cancel(notifyPendingIntent); mNotificationManager.cancelAll(); + + //Cancel the Pending Intent when you cancel the alarm. + //Not doing this can cause the toggle button to show up in the "ON" position even when the alarm is cancelled. + notifyPendingIntent.cancel(); //Set the toast message for the "off" case toastMessage = getString(R.string.alarm_off_toast);