Search code examples
androidalarmmanagerandroid-pendingintentandroid-context

Android: How to access a Pending Intent in a different activity?


I am setting up AlarmManager with a PendingIntent in activity A. Then I would like to be able to call cancel() on the same PendingIntent from a different activity B.

The Android documentation says the following:

"If the creating application later re-retrieves the same kind of PendingIntent (same operation, same Intent action, data, categories, and components, and same flags), it will receive a PendingIntent representing the same token if that is still valid, and can thus call cancel() to remove it."

Is it possible to store a PendingIntent in a database and retrieve it from a different activity?


Solution

  • If you are not creating your PendingIntent in a dynamic way, then you just need to create it the same way.

    I would recommend creating a singleton that will create and deliver your PendingIntent. That way, you could call something like this from anywhere in your application:

    PendingIntent pi = PendingIntentHelper.getInstance().buildPendingIntent();
    am.cancel(pi);