How can I click a notification, bring my app to the foreground if needed, and keep the stack as it was (even when the app went to the background)?
I have a NotificationClickActivity
that is launched when the user clicks a notification.
When the notification is clicked, I have two possible scenarios:
- User is logged out from the app
- User is logged in.
In the first scenario, NotificationClickActivity
starts the login process and receives the result. If OK, I launch MainActivity
. The task is cleared so MainActivity
is the only activity that I have in the task. I have no problems in this scenario.
In the second scenario NotificationClickActivity
does this:
finish()
startActivity(MainActivity.createIntent(this).setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP).putExtra("notification", notification))
With the above code, if the app is already running (MainActivity
is the root and top activity) and if on the background or in the foreground, I have no problems. MainActivity
onNewIntent
is called and I can do whatever I what with the notification. In this case, I dispatch the notification to a repository.
The problem I'm facing is when I have activities on top of MainActivity
. Imagine the following stack:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
With the code that I currently have if I click the notification the stack becomes:
MainActivity -> ActivityOne -> ActivityTwo -> ActivityN -> MainActivity
I want the stack to stay MainActivity -> ActivityOne -> ActivityTwo -> ActivityN
but still pass the notification to MainActivity
so it can be dispatched to the repository.
Any idea how can I achieve this?
I've been playing with the intent flags for some time but without success. Of course, I what this without any visible animations or whatever so that the user does not realize what is going on.