bring_app_to_foreground
Bring your Flutter app back to the foreground from the background on Android, even when the screen is off or locked.
Use it when something happens in the background and the user needs to see your
UI right away without touching the phone: an incoming call, an alarm, a
critical alert, a new job or order. Your background code calls
bringToFront(), your app's task moves to the front, the screen wakes, and
your UI shows over the lockscreen.
Tested on Android 14 and 15.
Why you need a permission
Since Android 10, apps in the background are not allowed to start activities
(background activity launch restrictions). A plain moveTaskToFront() from a
socket callback or a push handler is silently ignored. Full-screen intent
notifications are not a reliable way around it either: on Android 14+ that
permission is only granted by default to calling and alarm apps.
What a normal app can rely on is the SYSTEM_ALERT_WINDOW permission ("Display
over other apps"). The system exempts apps that hold it from the background
launch restrictions, so the app can bring its own task to the front, locked or
unlocked, over any other app.
The catch is that the user has to turn it on by hand from a system settings page. There is no runtime dialog for it.
So the plugin does two things:
- Checks and requests the "Display over other apps" permission.
- Brings the app to the front (relaunches its own task with
REORDER_TO_FRONT, wakes the screen and shows over the lockscreen).
Platform support
| Platform | Behavior |
|---|---|
| Android | Supported, API 24 (Android 7.0) and up. |
| iOS | No-op. iOS has no public API to bring an app to the front. |
| Web | No-op. |
| Desktop | No-op. |
All methods are safe to call on any platform and never throw. Off Android,
bringToFront() and the permission methods return false.
Install
dependencies:
bring_app_to_foreground: ^0.1.2
import 'package:bring_app_to_foreground/bring_app_to_foreground.dart';
There is nothing to add to your AndroidManifest.xml. The plugin declares
SYSTEM_ALERT_WINDOW and REORDER_TASKS in its own manifest and Android merges
them into your app at build time. No Gradle changes, no MainActivity changes,
no extra dependencies.
Usage
// 1. Check whether the permission is granted.
final granted = await BringAppToForeground.hasPermission();
// 2. If not, open the settings page so the user can turn it on.
// Explain why first (see "Asking for the permission" below).
if (!granted) {
await BringAppToForeground.requestPermission();
}
// 3. When a background event needs the app on screen:
await BringAppToForeground.bringToFront();
Example: incoming call
/// Call this from your push, socket or VoIP handler when a call arrives.
Future<void> onIncomingCall(Call call) async {
if (await BringAppToForeground.hasPermission()) {
await BringAppToForeground.bringToFront();
}
// If the permission is not granted, fall back to a heads-up or full-screen
// notification. That part lives in your app, not in this plugin.
showIncomingCallScreen(call);
}
Re-check when the user comes back
requestPermission() opens a system settings page and the user leaves your app
to flip the switch. The value it returns is the status before they come back,
so re-check when the app resumes:
class _MyState extends State<MyWidget> with WidgetsBindingObserver {
bool _granted = false;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_refresh();
}
@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
if (state == AppLifecycleState.resumed) _refresh();
}
Future<void> _refresh() async {
final granted = await BringAppToForeground.hasPermission();
if (mounted) setState(() => _granted = granted);
}
}
A complete runnable version is in example/.
Asking for the permission
"Display over other apps" is a sensitive permission, so:
- Ask for it as an optional step. Your app should keep working if the user says no, it just won't come to the front on its own.
- Show a short explanation before opening the settings page, for example "so
incoming calls can wake your screen", then call
requestPermission(). - If you publish on Google Play, describe the feature in your listing and be ready to explain why you need the permission.
Limitations
The plugin moves your app's existing task to the front. It does not keep your app alive or start it from scratch.
- The user has to grant the permission. Until they do, Android blocks the
launch and
hasPermission()returnsfalse. - Your app has to be running. If the process was killed or swiped away from
recents, there is no Dart code left to call
bringToFront(). Start your code first with a high-priority FCM data message, a foreground service or similar, then callbringToFront()from there. - An Activity has to be attached. If you call
bringToFront()from a context without one (for example a plain background isolate), it logs a warning and returnsfalse. - Notifications are up to you. Showing a call screen on the lockscreen through a full-screen intent notification is separate and out of scope here.
- Android only. Other platforms are no-ops.
API
| Method | Returns | Description |
|---|---|---|
bringToFront() |
Future<bool> |
Wake the screen, show over the lockscreen and bring the app's task to front. |
hasPermission() |
Future<bool> |
Whether "Display over other apps" is granted. false off Android. |
requestPermission() |
Future<bool> |
Open the settings page. Returns the status before the user comes back. |
bringToFront() returns true when the request was handed to Android and
false when it could not be made (no attached Activity, not Android, or an
error). A true result does not guarantee the app came to the front: without
the permission, Android can still ignore it.
How it works
When you call bringToFront(), the plugin uses the current Activity to:
- Call
setShowWhenLocked(true)andsetTurnScreenOn(true)(or the older window flags below API 27) so the app shows over the lockscreen. - Start the app's own launch intent (
getLaunchIntentForPackage) withFLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_REORDER_TO_FRONT | FLAG_ACTIVITY_SINGLE_TOP. - Call
moveTaskToFront()on its task as a fallback.
The plugin is ActivityAware, so it always works with the Activity that is
currently attached and never needs to know your MainActivity class.
License
MIT, see LICENSE.