-
Notifications
You must be signed in to change notification settings - Fork 33
Fix for Fragment having constructor with parameters. #986
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
Conversation
…ew constructor with params
| * // Old (will crash): | ||
| * val fragment = IterableEmbeddedView(viewType, message, config) | ||
| * | ||
| * // New (production-ready): | ||
| * val fragment = IterableEmbeddedView.newInstance(viewType, message, config) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will crash and new production ready can be changed with more subtle professional tone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow a dedicated class for arguments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah that was a lot of variables and it really clustered the fragment, let me know if it is ok
Ayyanchira
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. Added a nit pick
Ayyanchira
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
🔹 Jira Ticket(s) if any
✏️ Description
Root Cause
The
IterableEmbeddedViewfragment was using a constructor with parameters, which violates Android's Fragment lifecycle requirements. This causes crashes when the Android system attempts to recreate the fragment.Why This Happens
According to the official Android documentation:
When Android recreates fragments (during configuration changes, process death, or back stack operations), it uses reflection to call the empty constructor only. Any parameters passed through a custom constructor are lost, leaving properties uninitialized and causing crashes.
Official Android Best Practices
From the Android documentation on Creating a Fragment:
The Fix
Refactored
IterableEmbeddedViewto have newInstance with parameters and then bundle them up.Deprecating the fragment with arguments instead of making everything nullable, this way the variables better represent what is actually there.
The deprecation makes clients swap to the newInstance, but they can do with 1 click