🔄 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
Post a Comment