androidandroid-intentfirebase-cloud-messagingactivity-stack

How can I clear activities stack when user tap on FCM notification message?


My android app receives FCM notifications messages (display messages) from web server.

If the notification arrives while app is in background mode, and the user tap on it, a new "MainActivity" is launched on top of task stack. Then, when the user taps the back button, this activity is finished, and appears the last activity launched (before notification was tapped).

What I want is to clear all activity stack when user tap into notifications. This way, when user tap on the back button, the app will finish, preserving Activity hierarchy.

This could be easy if I manage the intent launch as described here, but as described in FCM Message types docs, display messages are handled by the FCM SDK automatically, so in this case I'm not creating any intent; android instead launch automatically the "MainActivity" when notification is tapped.

So, is there any way to achieve it?


Solution

  • For me, this worked perfect:

    <activity
    android:name=".MainActivity"
    android:clearTaskOnLaunch="true"
    android:launchMode="singleTask">