From 115594052aad8eb322a86fd72627c4cfbcc16e03 Mon Sep 17 00:00:00 2001 From: micro Date: Fri, 30 Jan 2026 10:34:26 +0300 Subject: [PATCH 1/2] fix(android): temporary files were deleted before share completed --- CHANGELOG.md | 5 +++++ README.md | 9 +++++++++ lib/esys_flutter_share_plus.dart | 34 +++++++++++++++++++++----------- pubspec.yaml | 2 +- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5aedd85..18a28b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# 2.5.1 + +* **Fixed** Android: an issue where files were deleted before being shared to another activity. +* **Added** public method `clean()` to trigger the cleanup of the temporary files directory. + ## 2.5.0 * Update Android `compileSdkVersion` to 35 diff --git a/README.md b/README.md index dc07e35..2f19637 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,15 @@ end Instead, if you have already a non-swift project, you can check this issue to solve the problem: [Friction adding swift plugin to objective-c project](https://github.com/flutter/flutter/issues/16049). +## IMPORTANT Note for Android +On Android, the Share function launches by default in a separate system task and activity. This maintains the cleanliness of your application's navigation stack. However, in this mode, the application cannot wait for the process to complete or reliably receive its result. + +Therefore, we cannot determine precisely whether the user has finished sharing the file to safely delete the temporary files. As a result, the temporary file directory is cleared each time a new sharing operation is initiated. + +You can also force a cleanup when you are certain the user has finished with the Share function (for example, when leaving the screen of your app from which sharing was launched). To do this, use the `clean()` method. + +Alternative option: You can disable launching in a separate system activity by setting the parameter `useSeparateActivity: false` in the `init()` method. In this case, the result of the Share operation will be returned to your application, and the cleanup of temporary files will happen automatically. + ## Usage Import: diff --git a/lib/esys_flutter_share_plus.dart b/lib/esys_flutter_share_plus.dart index 542da1b..ba05b44 100644 --- a/lib/esys_flutter_share_plus.dart +++ b/lib/esys_flutter_share_plus.dart @@ -13,6 +13,8 @@ class Share { static Future? _initialise; + static bool _useSeparateActivity = true; + /// Initializes the Share plugin. /// /// This method should be called at app startup or before starting to work with sharing. @@ -24,9 +26,14 @@ class Share { /// (e.g., in main() or in the initState of your main widget) to ensure /// a clean state before any sharing operations. static Future init({bool useSeparateActivity = true}) async { - await (_initialise ??= _init(useSeparateActivity)); + _useSeparateActivity = useSeparateActivity; + await (_initialise ??= _init()); + await _clearTempShareDirectory(); } + /// Clearing the directory with temporary files. + static Future clear() => _clearTempShareDirectory(); + /// Sends a text to other apps. static void text(String title, String text, String mimeType) { Map argsMap = { @@ -93,7 +100,7 @@ class Share { }; _channel.invokeMethod('file', argsMap).whenComplete(() { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) file.delete(); }); } @@ -137,8 +144,10 @@ class Share { 'filePaths': filePaths, }; _channel.invokeMethod('files', argsMap).whenComplete(() { - for (final file in tempFilesList) { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) { + for (final file in tempFilesList) { + file.delete(); + } } }); } @@ -168,7 +177,7 @@ class Share { }; _channel.invokeMethod('file', argsMap).whenComplete(() { - destFile.delete(); + if (!_useSeparateActivity || Platform.isIOS) destFile.delete(); }); } @@ -212,22 +221,23 @@ class Share { }; _channel.invokeMethod('files', argsMap).whenComplete(() { - for (final file in tempFilesList) { - file.delete(); + if (!_useSeparateActivity || Platform.isIOS) { + for (final file in tempFilesList) { + file.delete(); + } } }); ; } - static Future _init(bool useSeparateActivity) async { + static Future _init() async { if (Platform.isAndroid) { - await _initNativeAndroid(useSeparateActivity); + await _initNativeAndroid(); } - _clearTempShareDirectory(); } - static Future _initNativeAndroid(bool useSeparateActivity) async { - _channel.invokeMethod('init', useSeparateActivity); + static Future _initNativeAndroid() async { + _channel.invokeMethod('init', _useSeparateActivity); } static Future _getDirectoryForShareFile() async { diff --git a/pubspec.yaml b/pubspec.yaml index a13e95c..05ae997 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: esys_flutter_share_plus description: A Flutter plugin for sharing images & text with other applications. Fork by Innim -version: 2.5.0 +version: 2.5.1 homepage: https://github.com/Innim/esys-flutter-share repository: https://github.com/Innim/esys-flutter-share issue_tracker: https://github.com/Innim/esys-flutter-share/issues From b7c12799a32471e3df06f662f9ca1f79fca2918a Mon Sep 17 00:00:00 2001 From: micro Date: Fri, 30 Jan 2026 14:04:48 +0300 Subject: [PATCH 2/2] set `_useSeparateActivity` once on initializing --- lib/esys_flutter_share_plus.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/esys_flutter_share_plus.dart b/lib/esys_flutter_share_plus.dart index ba05b44..ad28b4f 100644 --- a/lib/esys_flutter_share_plus.dart +++ b/lib/esys_flutter_share_plus.dart @@ -26,8 +26,7 @@ class Share { /// (e.g., in main() or in the initState of your main widget) to ensure /// a clean state before any sharing operations. static Future init({bool useSeparateActivity = true}) async { - _useSeparateActivity = useSeparateActivity; - await (_initialise ??= _init()); + await (_initialise ??= _init(useSeparateActivity)); await _clearTempShareDirectory(); } @@ -230,7 +229,8 @@ class Share { ; } - static Future _init() async { + static Future _init(bool useSeparateActivity) async { + _useSeparateActivity = useSeparateActivity; if (Platform.isAndroid) { await _initNativeAndroid(); }