Search code examples
androidandroid-pendingintentlint

How to resolve "Missing PendingIntent mutability flag" lint warning in android api 30+?


As soon as I updated the target SDK to 30+ (Android R or later), a lint warning Missing PendingIntent mutability flag appeared on my PendingIntent.FLAG_UPDATE_CURRENT flag when I want to define PendingIntent.

How should I handle this lint with no effect on the app functionality?


Solution

  • You can set your pending intent as

    val updatedPendingIntent = PendingIntent.getActivity(
       applicationContext,
       NOTIFICATION_REQUEST_CODE,
       updatedIntent,
       PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT // setting the mutability flag 
    )
    

    According to the docs here: https://developer.android.com/about/versions/12/behavior-changes-12#pending-intent-mutability

    Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.

    Choose your flag accordingly.

    If you want to read more about this i would suggest that you read this great article here: https://medium.com/androiddevelopers/all-about-pendingintents-748c8eb8619