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.
Think you're looking for
FLAG_ACTIVITY_REORDER_TO_FRONT.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP || FLAG_ACTIVITY_REORDER_TO_FRONT)This will bring the activity to the top of the stack if its already running.
In the above example, if you want to go from A,B,C,D to A,B,C,D,B instead, then you need to make sure your activity supports multiple instances.
Suggest
FLAG_ACTIVITY_SINGLE_TOPto be specified in android manifest for the activity instead. Then in youronNewIntentyou should check if the user is logged in. If the user is not, you should launch the login process withstartActivityForResultand handle the login result withonActivityResult.This assumes your login can be initiated with an intent, which calls
setResultwith enough information for the caller to determine if the login was successful.As for animations, I think you will have to use fragments and disable animations, or disable animations for the activity, and do custom transitions only when you want it. This may even not work with activities, since the OS/OEM defined transitions may just happen with activities regardless anyway, so you'll very likely have to make sure rest of the app is in the same activity and use fragments for navigation.