When a user taps a push notification carrying a deep link while your Flutter app is already running, Android may recreate MainActivity instead of bringing the existing one to the front, which looks like the app restarting and loses the current session state.
Why it happens
The Pushwoosh Android SDK launches your app from a notification with the flags FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP | FLAG_ACTIVITY_SINGLE_TOP. With the default standard launch mode, CLEAR_TOP destroys the existing activity instance and creates a fresh one. Declaring an appropriate launch mode makes Android reuse the running instance and deliver the intent to onNewIntent() instead.
Fix
- Open
android/app/src/main/AndroidManifest.xml. - Find the
<activity>tag for.MainActivity. - Add
android:launchMode="singleTop".
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize">
<!-- ... other settings and intent-filters ... -->
</activity>
singleTop is what the official Pushwoosh Flutter example app ships with, and it is also the Flutter project template default. Combined with the SDK's CLEAR_TOP | SINGLE_TOP flags it reuses the running activity and routes the notification payload through onNewIntent().
singleTask also prevents the restart, but it changes task-affinity behaviour for the whole activity and can produce surprising back-stack results with other deep-link and browser-return flows. Prefer singleTop unless you have a specific reason to need a dedicated task.
Make sure your Dart code handles a deep link arriving on an already-running app (for example via the onPushAccepted listener or your deep-link package's stream), not only on cold start.
Comments
0 comments
Please sign in to leave a comment.