capacitor
capacitor copied to clipboard
fix(android): preserve file chooser callback across activity recreation
Fixes #8246
Description
When the Android system destroys and recreates the Activity while a file picker is open (due to backgrounding or memory pressure), the ValueCallback reference is lost, causing file uploads to fail silently.
This fix stores the file chooser callbacks in static variables so they survive activity recreation. When the activity result is received:
- If the instance callback exists, it's used normally
- If the instance callback is null, the static pending callback is used
Changes
- Added static variables to store pending file path callback, image URI, and file chooser type
- Added
FileChooserTypeenum to track which picker was opened - Added handlePendingFileChooserResult() to handle results when activity was recreated
- Added clearPendingFileChooserState() to clean up static state
- Updated showImageCapturePicker(), showVideoCapturePicker(), and showFilePicker() to store callbacks before launching
Hey @Suryansh0910, @zgl612, are you able to share a sample application (GitHub repo) with which you reproduced the issue + tested these fixes?. I think it will be tougher to get this merged without it.
Thanks!