-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix Android crash when changing shared Drawable tint on Searchbar #33071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
🚀 Dogfood this PR with:
curl -fsSL https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.sh | bash -s -- 33071Or
iex "& { $(irm https://raw.githubusercontent.com/dotnet/maui/main/eng/scripts/get-maui-pr.ps1) } 33071" |
|
Hey there @@tritter! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed. |
@dotnet-policy-service agree company="Thom Ritterfeld" |
|
/azp run MAUI-public |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Review Feedback: PR #33071 - Fix Android crash when changing shared Drawable tint on SearchbarRecommendation✅ Approve with Minor Suggestions Critical Fix: This PR correctly addresses a real Android crash caused by modifying shared Drawable resources without mutation. The fix follows Android best practices and should be merged. Recommended changes (non-blocking):
📋 Full PR Review DetailsSummaryThis PR fixes a critical Android crash that occurs when changing SearchBar tint colors during theme transitions (light/dark mode). The crash happens because MAUI was modifying shared Drawable resources directly, violating Android's documented threading/state management rules. The fix introduces a Impact: Prevents app crashes in production apps with multiple SearchBars when users toggle system theme. Root Cause AnalysisThe ProblemAndroid's resource system shares What was happening: // OLD CODE - DANGEROUS
searchMagIconImage?.Drawable?.SetTint(color); // Modifies shared resource!If multiple SearchBars reference the same drawable resource:
Why It's Hard to ReproduceThe issue requires all these conditions simultaneously:
This explains why:
The SolutionAndroid's documented fix: Call // NEW CODE - SAFE
var safe = drawable.Mutate(); // Creates private copy
safe.SetTint(color); // Modifies only this copy
imageView?.SetImageDrawable(safe); // Set the mutated copyFrom Android docs (Drawable.Mutate()):
Code ReviewChanges Made1. New Helper Method: internal static void SafeSetTint(ImageView? imageView, Color color)
{
if (imageView?.Drawable is not Drawable drawable)
return;
var safe = drawable.Mutate();
safe.SetTint(color);
imageView?.SetImageDrawable(safe);
}Why this is correct:
2. Replaced All Unsafe Tint Operations
Additional improvements:
Code Quality AssessmentStrengths:
Architecture:
Performance:
Security:
Test CoverageCurrent PR: ❌ No new tests included Why no tests? The author correctly notes:
Understanding the challenge:
Existing test coverage:
Risk assessment:
Verdict: Tests would be nice but not critical. The fix is straightforward and follows Android docs. Edge Cases AnalysisHandled Correctly ✅
Potential Concerns (Low Priority) 🟡
Consistency with MAUI CodebaseComparison with Previous FixIssue #30601 (July 2025): "SearchBar does not update colors on theme change"
This PR: Completes the theme change fix by adding proper drawable mutation. Issues FoundMust FixNone - Code is correct as-is. Should Fix (Non-blocking suggestions)
Won't Fix (Intentional choices)
Approval Checklist
Additional NotesFor ReviewersThis is a production crash fix. The author has:
Confidence level: Very High
For Merge DecisionRecommendation: Approve and merge Why merge:
Follow-up work (separate issue):
Review Metadata
Summary for TeamThis is a straightforward Android crash fix that should be merged. The author identified a real issue (modifying shared Drawables without mutation) and implemented the correct Android-recommended solution. The fix is minimal, follows best practices, and has been verified in production. Decision: ✅ Ready to merge |
|
Hi @tritter approved! If you could maybe add a comment that the pr-reviewer agent suggested, that would be perfect |
App crashes because of changing a shared Drawable on the fly. I found out it is the SearchBar. It only happens on Android and in large apps with lots of screens. Unfortunately I can't reproduce it in the TestSuite. But by reading the docs of Android this crash is common and caused by changing a Drawable which is still referenced. With a custom SearchBarHandler the issues is resolved.
Description of Change
Issues Fixed
Issue is fixed with a custom handler for now.
Fixes #
33070