I am using a PendingIntent along with AlarmManager and a BroadcastReceiver to show reminders at a user specified time. I am displaying the reminder using the NotificationManager.
Before a notification is set, I want a button to say “Set Reminder” and after the notification is set I want the button to say “Change Reminder”. As soon as the notification is shown to the user, the button should again say “Set Reminder”.
I am creating the same PendingIntent with the same intent, context and the same unique id (myUniqueId) to check whether the PendingIntent is active.
Intent intent = new Intent(context, ReminderReceiver.class);
boolean reminderActive = (PendingIntent.getBroadcast(context, myUniqueId, intent, PendingIntent.FLAG_NO_CREATE) != null);
Now this works and the button text is displayed properly. But I figured out that as soon as the notification is shown, unless I explicitly retrieves the same PendingIntent and cancels it, it’s NOT removed for a while. So for sometime, the button still says “Change Reminder”.
So is it correct to assume that unless I explicitly cancels the PendingIntent it’s still stored somewhere in the memory to be garbage collected?
Yes. Android caches
PendingIntentobjects. AFAIK, they will hang around until the process is terminated.