diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt index fb6a107988..c29cca474f 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/Share.kt @@ -66,6 +66,7 @@ internal class Share( val paths = (arguments["paths"] as List<*>?)?.filterIsInstance() val mimeTypes = (arguments["mimeTypes"] as List<*>?)?.filterIsInstance() val fileUris = paths?.let { getUrisForPaths(paths) } + val useNewTask = arguments["useNewTask"] as Boolean; // Create Share Intent val shareIntent = Intent() @@ -148,6 +149,13 @@ internal class Share( } } + if (useNewTask) { + chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + chooserIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK) + } else if (activity == null) { + chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + } + // Launch share intent startActivity(chooserIntent, withResult) } @@ -160,7 +168,6 @@ internal class Share( activity!!.startActivity(intent) } } else { - intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) if (withResult) { // We need to cancel the callback to avoid deadlocking on the Dart side manager.unavailable() diff --git a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart index c4ece10e00..256de1d3ff 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/method_channel/method_channel_share.dart @@ -41,6 +41,7 @@ class MethodChannelShare extends SharePlatform { if (params.subject != null) 'subject': params.subject, if (params.title != null) 'title': params.title, if (params.uri != null) 'uri': params.uri.toString(), + 'useNewTask': params.useNewTask, }; if (params.sharePositionOrigin != null) { diff --git a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart index 40ceee27b6..26b4341b39 100644 --- a/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart +++ b/packages/share_plus/share_plus_platform_interface/lib/platform_interface/share_plus_platform.dart @@ -142,6 +142,12 @@ class ShareParams { /// Parameter ignored on other platforms. final List? excludedCupertinoActivities; + /// Whether to use a new task when sharing on Android. + /// + /// * Supported platforms: Android + /// Parameter ignored on other platforms. + final bool useNewTask; + ShareParams({ this.text, this.subject, @@ -154,6 +160,7 @@ class ShareParams { this.downloadFallbackEnabled = true, this.mailToFallbackEnabled = true, this.excludedCupertinoActivities, + this.useNewTask = false, }); } diff --git a/packages/share_plus/share_plus_platform_interface/test/share_plus_platform_interface_test.dart b/packages/share_plus/share_plus_platform_interface/test/share_plus_platform_interface_test.dart index 18f22cf889..da1ef5dce3 100644 --- a/packages/share_plus/share_plus_platform_interface/test/share_plus_platform_interface_test.dart +++ b/packages/share_plus/share_plus_platform_interface/test/share_plus_platform_interface_test.dart @@ -71,6 +71,7 @@ void main() { ); verify(mockChannel.invokeMethod('share', { 'uri': 'https://pub.dev/packages/share_plus', + 'useNewTask': false, 'originX': 1.0, 'originY': 2.0, 'originWidth': 3.0, @@ -89,6 +90,7 @@ void main() { verify(mockChannel.invokeMethod('share', { 'text': 'some text to share', 'subject': 'some subject to share', + 'useNewTask': false, 'originX': 1.0, 'originY': 2.0, 'originWidth': 3.0, @@ -112,6 +114,7 @@ void main() { 'mimeTypes': ['image/png'], 'subject': 'some subject to share', 'text': 'some text to share', + 'useNewTask': false, 'originX': 1.0, 'originY': 2.0, 'originWidth': 3.0, @@ -127,6 +130,7 @@ void main() { verify(mockChannel.invokeMethod('share', { 'paths': [fd.path], 'mimeTypes': ['image/png'], + 'useNewTask': false, })); }); }); @@ -141,6 +145,7 @@ void main() { verify(mockChannel.invokeMethod('share', { 'paths': [fd.path], 'mimeTypes': ['*/*'], + 'useNewTask': false, })); }); }); @@ -190,6 +195,7 @@ void main() { verify(mockChannel.invokeMethod('share', { 'text': 'some text to share', 'subject': 'some subject to share', + 'useNewTask': false, 'originX': 1.0, 'originY': 2.0, 'originWidth': 3.0, @@ -206,6 +212,7 @@ void main() { verify(mockChannel.invokeMethod('share', { 'paths': [fd.path], 'mimeTypes': ['image/png'], + 'useNewTask': false, })); expect(result, success); });