@@ -13,17 +13,27 @@ should add it to your `pubspec.yaml` as usual.
13
13
14
14
## Usage with launcher activities
15
15
16
- If your app implements an activity that launches the main ` FlutterActivity `
17
- (` MainActivity.java ` /` MainActivity.kt ` by default), then you may need to change
18
- the launch mode of your launcher activity and/or modify the ` Intent ` flags used
19
- to launch the main ` FlutterActivity ` in order to achieve the desired back press
20
- behavior and task management.
21
-
22
- If your launcher activity only launches the main ` FlutterActivity ` without any
23
- additional relevant logic (like the code sample provided in the description of
24
- https://github.com/flutter/flutter/issues/152883 ), to have your app maintain the
25
- same behavior of ` quick_actions_android ` with/without a launcher activity, set
26
- the launch mode of your launcher activity to ` singleInstance ` in
16
+ If you have an activity that launches a ` FlutterActivity ` (this is
17
+ ` MainActivity.java ` /` MainActivity.kt ` by default), then you might need to
18
+ modify the launch configuration of that activity to have the back press
19
+ behavior and task back stack that you expect. Common use cases of having
20
+ such a launcher activity are in an add to app project or if your Flutter
21
+ project contains multiple Android activities.
22
+
23
+ For example, consider the case where you have two different quick actions
24
+ shortcuts for your app and a launcher activity that launches the
25
+ ` FlutterActivity ` . If the launcher activity uses the [ ` singleTop ` ] [ 4 ] launch
26
+ mode (as Flutter's default ` MainActivity.java ` /` MainActivity.kt ` do by default)
27
+ and the user
28
+
29
+ 1 . Launches your app from the first shortcut
30
+ 2 . Moves your app into the background by exiting the app
31
+ 3 . Re-launches your app from the second shortcut
32
+
33
+ then the user will see what the first shortcut launched, not what the second
34
+ shortcut was supposed to launch. To fix this, you may set the launch mode of
35
+ the launcher activity to ` singleInstance ` (see [ Android documentation] [ 5 ] for
36
+ more information on this mode) in
27
37
` your_app/android/app/src/mainAndroidManifest.xml ` :
28
38
29
39
``` xml
@@ -32,8 +42,37 @@ the launch mode of your launcher activity to `singleInstance` in
32
42
android : launchMode =" singleInstance" >
33
43
```
34
44
35
- See the [ Tasks and the back stack] [ 4 ] Android documentation for more information
36
- on the different launch modes and ` Intent ` flags you may need.
45
+ See [ this issue] [ 6 ] for more context on this exact scenario and its solution.
46
+
47
+ Depending on your use case, you may additionally need to set the proper launch
48
+ mode ` Intent ` flags in the ` Intent ` that launches the ` FlutterActivity ` to
49
+ achieve your expected back press behavior and task back stack. For example,
50
+ if ` MainActivity.java ` is the ` FlutterActivity ` that your launcher activity
51
+ launches:
52
+
53
+ ``` java
54
+ public final class LauncherActivity extends Activity {
55
+
56
+ @Override
57
+ protected void onCreate (Bundle savedInstanceState ) {
58
+ super . onCreate(savedInstanceState);
59
+
60
+ Intent mainActivityIntent = new Intent (this , MainActivity . class);
61
+ mainActivityIntent. putExtras(getIntent());
62
+
63
+ // Add any additional launch mode Intent flags you need:
64
+ mainActivityIntent. addFlags(... );
65
+
66
+ startActivity(mainActivityIntent);
67
+ finish();
68
+ }
69
+
70
+ ...
71
+ }
72
+ ```
73
+
74
+ See [ Tasks and the back stack] [ 5 ] for more documentation about the different
75
+ launch modes and related ` Intent ` flags that Android provides.
37
76
38
77
## Contributing
39
78
@@ -42,4 +81,6 @@ If you would like to contribute to the plugin, check out our [contribution guide
42
81
[ 1 ] : https://pub.dev/packages/quick_actions
43
82
[ 2 ] : https://flutter.dev/to/endorsed-federated-plugin
44
83
[ 3 ] : https://github.com/flutter/packages/blob/main/CONTRIBUTING.md
45
- [ 4 ] : https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes
84
+ [ 4 ] : https://developer.android.com/reference/android/content/Intent?authuser=1#FLAG_ACTIVITY_SINGLE_TOP
85
+ [ 5 ] : https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes
86
+ [ 6 ] : https://github.com/flutter/flutter/issues/152883#issuecomment-2305906933
0 commit comments