🐛 fix(android): resolve lifecycle ClassCastException in barcode scanner plugin#384
Open
giovanninibarbosa wants to merge 6 commits intoAmolGangadhare:masterfrom
Conversation
…ajor changes in a clear and organized way.
… plugin - Remove direct Lifecycle casting that fails in Flutter 3.29.3+ - Use Application.registerActivityLifecycleCallbacks instead - Update cleanup to properly unregister lifecycle observer - Remove unused androidx.lifecycle.Lifecycle import Fixes ClassCastException: HiddenLifecycleReference cannot be cast to androidx.lifecycle.Lifecycle
Author
|
This PR should be after #382 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Flutter Barcode Scanner Lifecycle Fix
🔍 Issue Summary
Fixed critical Android ClassCastException in
flutter_barcode_scannerplugin that was preventing app startup in Flutter 3.29.3+ environments.🚨 Problem Description
Error Details
Root Cause
The plugin was attempting to directly cast
activityBinding.getLifecycle()toandroidx.lifecycle.Lifecycle:In Flutter 3.29.3+ and other recent versions,
getLifecycle()returns aHiddenLifecycleReferencewrapper instead of a directLifecycleobject, causing theClassCastException.✅ Solution Implementation
Code Changes
1. Removed Direct Lifecycle Field
2. Updated Plugin Setup Method
3. Updated Cleanup Method
4. Cleaned Up Imports
📁 Files Modified
Primary Changes
flutter_barcode_scanner/android/src/main/java/com/amolg/flutterbarcodescanner/FlutterBarcodeScannerPlugin.java🧪 Testing & Validation
Automated Checks
Manual Testing Recommended
🏗️ Technical Architecture
Modern Flutter Plugin Lifecycle Management
This fix adopts the modern approach for Flutter plugin lifecycle management:
Compatibility Matrix
🔒 Security & Performance Impact
Security
Performance
📋 Rollback Plan
If issues arise, rollback involves:
🔗 Related Issues & References
Similar Issues in Flutter Community
Best Practices Applied
📝 Change Summary
Priority: 🔴 Critical (Blocks app startup)
Complexity: 🟡 Medium (Requires Android knowledge)
Risk: 🟢 Low (Internal plugin change only)