Skip to content

Commit a566da6

Browse files
authored
[quick_actions_android] Update instructions for using a launcher activity with more breadcrumbs (flutter#7716)
Builds on flutter/packages#7686 to give more context about the issue I solved in flutter#152883 and hopefully leave better breadcrumbs for developers that find themselves wanting to use a launcher activity and the `quick_actions_android` plugin. Fixes flutter#152883.
1 parent 048ae84 commit a566da6

File tree

3 files changed

+61
-15
lines changed

3 files changed

+61
-15
lines changed

packages/quick_actions/quick_actions_android/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.0.17
2+
3+
* Updates README to include more specific context on how to use launcher activities, including
4+
a full explanation for https://github.com/flutter/flutter/issues/152883.
5+
16
## 1.0.16
27

38
* Updates README to include guidance on using the plugin with a launcher activity.

packages/quick_actions/quick_actions_android/README.md

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,27 @@ should add it to your `pubspec.yaml` as usual.
1313

1414
## Usage with launcher activities
1515

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
2737
`your_app/android/app/src/mainAndroidManifest.xml`:
2838

2939
```xml
@@ -32,8 +42,37 @@ the launch mode of your launcher activity to `singleInstance` in
3242
android:launchMode="singleInstance">
3343
```
3444

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.
3776

3877
## Contributing
3978

@@ -42,4 +81,6 @@ If you would like to contribute to the plugin, check out our [contribution guide
4281
[1]: https://pub.dev/packages/quick_actions
4382
[2]: https://flutter.dev/to/endorsed-federated-plugin
4483
[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

packages/quick_actions/quick_actions_android/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: quick_actions_android
22
description: An implementation for the Android platform of the Flutter `quick_actions` plugin.
33
repository: https://github.com/flutter/packages/tree/main/packages/quick_actions/quick_actions_android
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 1.0.16
5+
version: 1.0.17
66

77
environment:
88
sdk: ^3.4.0

0 commit comments

Comments
 (0)