-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Use view binding in fragments. #4814
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
Use view binding in fragments. #4814
Conversation
f46a5f6 to
8650962
Compare
21b90fc to
6c21e33
Compare
6c21e33 to
88e4595
Compare
|
@Isira-Seneviratne |
I already did that, did I miss a fragment? |
|
The same I said in #4762 (comment) applies here ;-) |
b31ca51 to
08bb597
Compare
|
@Stypox Bumping this for examination and merging, as it's been updated. |
08bb597 to
2d6831f
Compare
Stypox
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.
Code looks good, thank you! In the review I'm just asking clarifications for things I am not sure about.
I tested by navigating in many different fragments and everything looked good; I also took a heap dump with LeakCanary and the only leaks that were pointed out were unrelated to view binding. @TobiGr almost ready to be merged
| viewPager.setAdapter(null); | ||
| if (binding != null) { | ||
| binding.pager.setAdapter(null); | ||
| binding = null; |
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.
Why are you setting binding = null here and not in onDestroyView like all other fragments?
(unrelated to this PR, just a note for the future) pagerAdapter should be set to null here, as LeakCanary reports it as leaking.
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.
Because the pager adapter is being set to null in that method, and the onDestroyView method is called before onDestroy: https://developer.android.com/guide/fragments/lifecycle.
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.
That logic should be in onDestroyView, since the view gets re-created all the time.
onDestroy happens when the fragment is killed, what does that mean?
onDestroyView is called everytime whilst onDestroy isn't.
You should move it to onDestroyView
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.
Okay.
| private var _feedBinding: FragmentFeedBinding? = null | ||
| private val feedBinding get() = _feedBinding!! |
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.
Can't you just make them lateinit to prevent having to use asserting getters? Though I have little experience in Kotlin, so I may be wrong ;-)
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.
lateinit variables can't have null assigned to them, and the variable needs to be set to null to allow the binding to be garbage collected: https://developer.android.com/topic/libraries/view-binding#fragments.
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.
Ok, I see
| feedBinding.loadingProgressText.text = "${progressState.currentProgress}/${progressState.maxProgress}" | ||
| } else if (progressState.progressMessage > 0) { | ||
| loading_progress_text?.setText(progressState.progressMessage) | ||
| _feedBinding?.loadingProgressText?.setText(progressState.progressMessage) |
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.
Why are you not using feedBinding.loadingProgressText. here and below?
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.
I was getting a NullPointerException when using the non-null feedBinding property.
| private var _binding: FragmentSubscriptionBinding? = null | ||
| private val binding get() = _binding!! |
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.
Also here and in other places
|
@TobiGr didn't we also want to merge this before releasing 0.20.7? |
|
Yes, I forgot to add this PR to the "to do" section. |
2d6831f to
c8baef0
Compare
|
I get this binding leak with your latest apk, I don't know if it is a real leak. The heap dump was captured while I was in the VideoDetailFragment, if that counts anyhow: |
Maybe it's because I moved the |
|
@Isira-Seneviratne the apk I used is from the PR description, so from 4 days ago by looking at the edits, therefore it is before I did the review and you pushed the change so that's not the cause. Could you provide an updated apk? |
Sometimes LeakCanary has false positives, you can see that it cleary suggests that you should release all your resources in onDestryoView and if you do so then it's confused, but if you aren't doing it, then something's wrong. |
Sure. |
c8baef0 to
8735de7
Compare
8735de7 to
0e2e4db
Compare
…Header() and getListFooter() methods.
…r() and getListFooter() methods.
0e2e4db to
f04b5fd
Compare
Stypox
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.
I tested again with the apk from the CI and I couldn't reproduce the leak anymore. I also tested more but everything worked well. Thank you :-D
…e_view_binding_in_fragments Use view binding in fragments.
What is it?
Description of the changes in your PR
APK testing
debug.zip
Due diligence