Skip to content

fix(ui): prevent NPE and memory leaks in FileDetailActivitiesFragment#16465

Open
joshtrichards wants to merge 1 commit intomasterfrom
jtr/fix-ui-FileDetailsActivitiesFragment
Open

fix(ui): prevent NPE and memory leaks in FileDetailActivitiesFragment#16465
joshtrichards wants to merge 1 commit intomasterfrom
jtr/fix-ui-FileDetailsActivitiesFragment

Conversation

@joshtrichards
Copy link
Member

@joshtrichards joshtrichards commented Feb 7, 2026

Problem

SubmitCommentTask callbacks could execute after onDestroyView(), causing NPE crashes when accessing null binding. And AsyncTask was never cancelled, creating possible memory leaks.

Fixes #93511
Related prior PR context: #7210

Solution

  1. AsyncTask lifecycle management:
  • Added submitCommentTask field to track running task
  • Cancel task in onDestroyView() and before starting new submissions
  • Added isCancelled() checks in doInBackground() and onPostExecute()
  1. Prevent memory leaks:
  • Wrap callback in WeakReference<> in SubmitCommentTask
  • Set callback = null in onDestroyView()
  1. Lifecycle-aware callback:
  • Added binding != null && Lifecycle.State.RESUMED check in onSuccess()
  • Used getView() != null && isAdded() check in onError()
  1. Defensive null checks:
  • Added binding != null checks in: submitComment(), setLoadingMessage(), setLoadingMessageEmpty(), populateList(), setInfoContent(), hideRefreshLayoutLoader(), avatarGenerated()

Files Changed

  • FileDetailActivitiesFragment.java: Fixed callback lifecycle, added AsyncTask cancellation, added null safety checks

Testing

  • Fragment can be destroyed during comment submission without crashes
  • Multiple rapid submissions properly cancel previous operations
  • No memory leaks with pending operations

Note

  • Uses defensive null checks as immediate fix.
  • Future work: migrate from deprecated AsyncTask to coroutines.
  • Tests written, or not not needed

Footnotes

  1. https://github.com/nextcloud/android/blob/695079ef957a86523e6f67c5bc82ef0929c61200/src/main/java/com/owncloud/android/ui/fragment/FileDetailActivitiesFragment.java#L171-L174

- Cancel SubmitCommentTask in onDestroyView to prevent post-destruction callbacks
- Wrap callback in WeakReference to allow garbage collection
- Add lifecycle checks (RESUMED state) before accessing binding in callbacks
- Add defensive null checks for binding throughout fragment methods
- Check isCancelled() in AsyncTask to prevent unnecessary work after cancellation

Fixes crashes when fragment view is destroyed while comment submission is pending.

Fixes #9351

Signed-off-by: Josh <josh.t.richards@gmail.com>
@github-actions
Copy link

github-actions bot commented Feb 7, 2026

blue-Light-Screenshot test failed, but no output was generated. Maybe a preliminary stage failed.

@github-actions
Copy link

github-actions bot commented Feb 7, 2026

APK file: https://www.kaminsky.me/nc-dev/android-artifacts/16465.apk

qrcode

To test this change/fix you can simply download above APK file and install and test it in parallel to your existing Nextcloud app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant