Skip to content
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

Fix crash on configuration changes in DatePickerFragment #1431

Merged
merged 4 commits into from
Jul 19, 2023

Conversation

unbiaseduser
Copy link
Contributor

Fixes #1201. When you tab in and out of other apps or rotate your screen, the DatePickerFragment won't cause crashes anymore.
Let me explain how the fix works:

  • Fragments must have a public, no arguments constructor. Any data you want to pass to them must be put in a Bundle for Android to be able to persist it across configuration changes (like the situations above).
  • Communication between fragments or between an activity and a fragment is performed with FragmentManager.setFragmentResultListener() on the activity/fragment that receives the callback and FragmentManager.setFragmentResult() on the one that sends the callback. The callback data must fit in a Bundle.

In the original code, the date picker directly sets the target text field. Here, the code for setting the text field is put in a callback received by the host activity, and the date picker fragment's only responsibility is receiving input data (the Date objects) and sending output data (the picked Date). To know which text field to set, I pass in an "identifier" (which conveniently is the LoyaltyCardField enum that's already used in the "display date picker" method, so I don't have to create a new enum or something.)

Copy link
Member

@TheLastProject TheLastProject left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great and works well. Thanks for the fix and the detailed explanation! I just have a few nitpicks, nothing big :)

- Re-add accidentally removed comment 🤦‍♂️
- Move instance variables below constants and above static methods
- Move nested interface to top of class
- Remove "_key" suffix in constant
@TheLastProject
Copy link
Member

Thanks!

@TheLastProject TheLastProject merged commit 682fc83 into CatimaLoyalty:main Jul 19, 2023
@obfusk
Copy link
Contributor

obfusk commented Jul 19, 2023

Thanks for the fix and the explanation. Learned something new :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when returning if date picker was left open
4 participants