Android: When app was opened with click on notification, clicking on further notifications only opens the app, not changing activity

According to this thread on StackOverflow, “Android remembers the intent that was used to launch it”. The problem described there is exactly what I’m going through:

When the app is launched for the first time from a Notification, this can mess things up. You launch the app from a Notification and Android remembers the Intent used (from the Notification), when you later launch the app (again from a Notification), android matches the Intent in the Notification with the Intent used to launch the app for the first time, and thinks you want to bring the existing app task from the background to the foreground.

The problem: I don’t understand why Android thinks that the Intent is the same. While the activity is the same, the intent has completely different extras.

When I add flags to the intent, I’m able to fix this issue. Like for example:

Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK

But my goal is to keep the back stack, which it doesn’t using these flags of course.

It’s a webview app with some additional screens.

The issue only exists when the app is launched through a notification. When I launch it myself and put it to the background, everything works as I wish.

Scenario

  • not1 = notification with activity MainActivity and extra "openLink" = https://something.com/one
  • not2 = notification with activity OtherActivity and some extras
  • not3 = notification with activity MainActivity and extra "openLink" = https://something.com/two
  • app is closed.

What I want:

  1. user clicks on not1, app launches, opens MainActivity, opens link in webview
  2. user clicks on not2, app opens OtherActivity
  3. user clicks on not3, app opens MainActivity, opens link in webview
  4. user clicks “back”, OtherActivity from not2 is shown
  5. user clicks “back”, MainActivity from not1 is shown

How it is right now:

  1. user clicks on not1, app launches, opens MainActivity, opens link in webview ✅
  2. user clicks on not2, app opens OtherActivity ✅
  3. user clicks on not3, app opens and shows OtherActivity. Not calling onCreate(). If the app was in background, in calls onResume() of OtherActivity. Even though it was supposed to open MainActivity.

My code:

val pendingIntent = PendingIntent.getActivity(
    baseContext,
    notificationId,
    newIntent,
    PendingIntent.FLAG_IMMUTABLE
)

notificationBuilder = notificationBuilder.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notificationBuilder.build())

notificationId is unique. extras of newIntent are unique. I tried every flag there is I believe. MainActivity (which has the webview) has launchMode “singleTop”, but of course I tried without setting a launchMode.

Further observations:

  • If I set android:taskAffinity=”” and user clicks on not3 it works, but messes up the back stack. it just pulls the MainActivity in front of the backstack.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật