fix: UninitializedPropertyAccessException in ImageFragment onDestroy#6661
Conversation
|
Curious: when do we not initialise this property? What's the actual root cause for getting this exception? |
here the property remains uninitialized if the |
|
I don't get this crash, so I'm not sure if this is really causing the problem. I'll check on a few emulators and get back to you. |
steps :
I'll try to poste a screen record on how the crash is reproducble 🙂 |
|
I've seen the issue already and tried those steps, seen your recording as well. It's not reproducible on my device. |
ohh, i'll recheck once on other devices and let you know about it :) |
|
I tested this on freshly installing the app Screenrecorder-2026-02-22-10-47-48-458.mp4 |
|
We run this else block and initialise an empty array list: https://github.com/Kota-Jagadeesh/apps-android-commons/blob/32c6a8d6d474a44979f7de4b7d5b76ba43f4a64a/app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/ImageFragment.kt#L347. Why do we need a lateinit when we can initialise it? 🙂 |
yeah, right :). while the line 347 handles the empty case during the result, the race condition occurs because the |
|
I am planing to modify the property declaration in the imagefragment to initialise it immediately and remove the lateint keyword. from this : lateinit var filteredImages: ArrayList<Image>to this : var filteredImages: ArrayList<Image> = arrayListOf() |
|
By any chance, are we using it in other classes? If not, we can make it private. |
planning the same, and verified that |
|
Thanks! Just a tip: whenever you feel tempted to add a defensive check, think why we even need it and if there's a bigger problem. Defensive checks just hide such problems and we get surprises when they're unveiled somewhere else 🙂 |
|
✅ Generated APK variants! |
|
Tested on an emulator. Just to confirm - does pressing back now take you to the outer directory? |
yeah, tested again and the crash no more occurs, and it safely retuns to the custom selctorr view 🙂 and thanks for the tip @RitikaPahwa4444 , i'll definitely keep that in mind to prioritize fixing the root cause over the adding defensive layers in the future! |
|
@Kota-Jagadeesh, do we still need the line I'd pointed before: https://github.com/Kota-Jagadeesh/apps-android-commons/blob/32c6a8d6d474a44979f7de4b7d5b76ba43f4a64a/app/src/main/java/fr/free/nrw/commons/customselector/ui/selector/ImageFragment.kt#L347? We're initialising it in the beginning and not reassigning anywhere I guess? 🤔 |
yeah @RitikaPahwa4444, as |
|
I'm not sure if we need to clear the list or if it will be empty already, will have to check the code. |
from the |
Description (required)
Fixes #6660
The app was crashing with a
kotlin.UninitializedPropertyAccessExceptionwhen a user pressed the "Back" button while images were still loading in the Custom Selector.Changes made:
::filteredImages.isInitializedcheck in theonDestroymethod ofImageFragment.filteredImagesand save the last visible item ID if the background loading process has successfully completed and initialized the property.Tests performed (required)
Tested ProDebug on Redmi Note 13 Pro with API level 35 (Android 15).
Followed the steps mentioned in the issue #6660, and verified that the app no more crashes and safely returns to the custom selector after pressing back from the folder which i entereed.
Screenshots (for UI changes only)
N/A