When a user taps a push notification containing a deep link while your Flutter app is already open, Android might launch a new instance of the MainActivity. This can cause unexpected behavior and disrupt the user's session by effectively restarting the app.
This occurs because the default Intent used to open the app from the notification doesn't specify how to handle an existing app instance.
To resolve this and ensure the existing app instance is brought to the foreground, you need to modify the launchMode for your MainActivity.
Steps:
- In your Flutter project, navigate to the Android-specific files and open
android/app/src/main/AndroidManifest.xml. - Locate the
<activity>tag for your.MainActivity. - Add the attribute
android:launchMode="singleTask"inside this tag.
Example:
Your <activity> tag should look similar to this after the change:
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTask"
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>
By setting the launchMode to singleTask, you instruct the Android system to use the existing activity task instead of creating a new one, thus preventing the app from restarting.
Comments
0 comments
Please sign in to leave a comment.