๐Ÿ”„ FLAG_IMMUTABLE vs FLAG_MUTABLE IN ANDROID STUDIO

            ๐Ÿ”„ FLAG_IMMUTABLE vs FLAG_MUTABLE



FLAG_IMMUTABLE:-

                                          Use FLAG_IMMUTABLE agar aapko PendingIntent ke content ko modify nahi karna.


FLAG_MUTABLE:-

                              Use FLAG_MUTABLE agar aapko PendingIntent ko dynamically change karna hota hai (e.g. for inline replies in notifications)



๐Ÿ›  Pro Tip:

Jab bhi PendingIntent create karein, Android 12+ ke support ke liye flag zaroor set karein.



๐Ÿ”ด Your existing code (likely causing the crash):
Intent intent = new Intent(this, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); // ❌ No flag



Fixed version:
Intent intent = new Intent(this, SomeActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE); // ✅


Example:- 

PendingIntent pendingIntent = PendingIntent.getActivity(
    context, 
    requestCode, 
    intent, 
    PendingIntent.FLAG_IMMUTABLE // or FLAG_MUTABLE if really needed
);

If you're using PendingIntent.getBroadcast() or PendingIntent.getService() instead, the fix is the same — just add the appropriate flag.

Comments

Popular posts from this blog

๐Ÿ“˜ Top 500 Java Interview Questions (With Topics)

Git And GitHub Collaborators and teams

Android Interview Question and Answer