Skip to content

Fix IllegalArgumentException when dismissing ProgressDialog after lifecycle changes - #231

Draft
cursor[bot] wants to merge 1 commit into
mainfrom
illegalargumentexception-viewdecorviewbcf2619empowerplantactivity-not-78jcky
Draft

Fix IllegalArgumentException when dismissing ProgressDialog after lifecycle changes#231
cursor[bot] wants to merge 1 commit into
mainfrom
illegalargumentexception-viewdecorviewbcf2619empowerplantactivity-not-78jcky

Conversation

@cursor

@cursor cursor Bot commented Jun 25, 2026

Copy link
Copy Markdown

Description

This PR fixes an IllegalArgumentException that occurs when ProgressDialog.dismiss() is called after the activity/fragment has been destroyed or recreated (e.g., during device rotation or navigation).

Root Cause

The issue occurred because:

  1. ProgressDialog.dismiss() was being called from OkHttp network callbacks after EmpowerPlantActivity/MainFragment was destroyed/recreated
  2. The dialog's DecorView was no longer attached to the WindowManager when dismiss was called
  3. There were no lifecycle checks before dismissing the dialog
  4. A duplicate dismiss() call existed in the fetchToolsFromServer() method

Changes

  • Added dismissProgressDialogSafely() helper method that checks:
    • Fragment is still added (isAdded())
    • Fragment is not detached (!isDetached())
    • Dialog is showing (isShowing())
    • Wrapped dismiss in try-catch for edge cases
  • Added dismissCheckoutProgressDialogSafely() for the checkout flow
  • Ensured all dialog dismiss calls happen on the UI thread using runOnUiThread()
  • Removed duplicate progressDialog.dismiss() call in fetchToolsFromServer()
  • Renamed local progressDialog variable in checkout() to checkoutProgressDialog to avoid confusion

Testing

To reproduce the original issue:

  1. Open EmpowerPlantActivity which loads MainFragment
  2. While the "Loading..." ProgressDialog is showing, rotate the device or navigate away
  3. The activity/fragment is destroyed and recreated
  4. When the network response arrives, the old code would crash with IllegalArgumentException

With this fix, the dialog dismissal is safely skipped if the fragment is no longer attached.

Fixes ANDROID-EW

Open in Web Open in Cursor 

…ecycle changes

Add lifecycle-aware dialog dismissal to prevent crashes when the activity/fragment
is destroyed or recreated during async network calls.

Changes:
- Add dismissProgressDialogSafely() helper method that checks isAdded() and isDetached()
- Add dismissCheckoutProgressDialogSafely() for checkout flow
- Ensure all dialog dismiss calls happen on the UI thread
- Remove duplicate progressDialog.dismiss() call in fetchToolsFromServer
- Add try-catch blocks to handle edge cases during dismissal

Fixes [ANDROID-EW](https://demo.sentry.io/issues/7431031780/)
@sentry

sentry Bot commented Jun 25, 2026

Copy link
Copy Markdown

📲 Install Builds

Android

🔗 App Name App ID Version Configuration
Android com.example.vu.android 24.12.26 (241226) release
Android com.example.vu.android 24.12.26 (241226) debug
Android com.example.vu.android 24.12.26 (241226) release

⚙️ android Build Distribution Settings

@codecov

codecov Bot commented Jun 25, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 0% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 0.00%. Comparing base (fbd7a20) to head (eb7ae47).

Files with missing lines Patch % Lines
.../example/vu/android/empowerplant/MainFragment.java 0.00% 19 Missing ⚠️
Additional details and impacted files
@@          Coverage Diff          @@
##            main    #231   +/-   ##
=====================================
  Coverage   0.00%   0.00%           
=====================================
  Files         16      16           
  Lines        875     884    +9     
  Branches      65      65           
=====================================
- Misses       875     884    +9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

1 participant