Android: App Restarts on Notification Tap when Native Views are Open
#2712 opened on Oct 29, 2025
Description
When an Android application using the flutter_local_notifications package has a native view (e.g., Camera, File Explorer, or a third-party Native Activity like an Offerwall) open on top of the FlutterActivity, tapping a local notification causes the entire application to restart instead of resuming the existing FlutterActivity instance.
This behavior occurs because the Intent created by the plugin to launch the app often includes flags that instruct the Android OS to launch the Activity in a new task or clear the existing state, leading to the destruction and recreation of the Flutter Engine.
Behavior Observed Flutter App is running.
A Native Activity (e.g., Camera or PincruxOfferwallViewController) is launched on top of the FlutterActivity.
A local notification is received and tapped.
Expected Behavior: The Native Activity closes (or pops off the stack), and the existing FlutterActivity instance resumes, calling onNewIntent or related lifecycle methods. The Flutter state is preserved.
Actual Behavior: The entire Flutter application restarts, the engine is reinitialized, and the Flutter state is lost.
Proposed Solution / Feature Request The restart issue can be reliably prevented by adding the FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP flags to the Intent that launches the main activity (getLaunchIntent(context)).
We request the addition of an optional parameter in AndroidInitializationSettings or similar options to allow developers to specify these flags, particularly for the Intent created inside the createNotification method.
- Necessary Code Modification The modification is required in the protected static Notification createNotification(...) method within the Android source (e.g., FlutterLocalNotificationsPlugin.java):
//////////////////// protected static Notification createNotification( Context context, NotificationDetails notificationDetails) { // ... (setup notification channel)
Intent intent = getLaunchIntent(context); intent.setAction(SELECT_NOTIFICATION); intent.putExtra(NOTIFICATION_ID, notificationDetails.id); intent.putExtra(PAYLOAD, notificationDetails.payload);
// ⭐ Proposed Fix: Add these flags to prevent application restart // This combination ensures the Activity is brought to the top, clears // anything above it, and prevents its recreation. intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
int flags = PendingIntent.FLAG_UPDATE_CURRENT; // ... (rest of the method) } //////////////////////////////
- Feature Request Please consider adding a new optional parameter (e.g., intentFlags) to the Dart configuration (AndroidInitializationSettings) that maps to the Android Intent flags, allowing developers to set Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP without modifying the plugin source code.
Thank you for your consideration!