Skip to content

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

Merged
alperozturk96 merged 1 commit intomasterfrom
jtr/fix-ui-FileDetailsActivitiesFragment
Feb 9, 2026
Merged

fix(ui): prevent NPE and memory leaks in FileDetailActivitiesFragment#16465
alperozturk96 merged 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>
@alperozturk96 alperozturk96 force-pushed the jtr/fix-ui-FileDetailsActivitiesFragment branch from 7f2e929 to f971397 Compare February 9, 2026 08:29
Copy link
Collaborator

@alperozturk96 alperozturk96 left a comment

Choose a reason for hiding this comment

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

Hello

Thank you for the PR. Submitting comments is working; however, displaying them is not. This issue is out of scope for the current PR, as the master branch exhibits the same behavior.

@alperozturk96 alperozturk96 merged commit c1d88a8 into master Feb 9, 2026
14 of 20 checks passed
@alperozturk96 alperozturk96 deleted the jtr/fix-ui-FileDetailsActivitiesFragment branch February 9, 2026 08:37
@github-actions
Copy link

github-actions bot commented Feb 9, 2026

Codacy

SpotBugs

CategoryBaseNew
Bad practice4242
Correctness7474
Dodgy code257257
Experimental11
Internationalization77
Malicious code vulnerability22
Multithreaded correctness3434
Performance4344
Security1818
Total478479

SpotBugs increased!

@github-actions
Copy link

github-actions bot commented Feb 9, 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.

@github-actions
Copy link

github-actions bot commented Feb 9, 2026

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

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

Projects

None yet

2 participants