Android: Control which activity is shown when clicking notification
In an Android app, I have two activities; lets call them A and B. A is the main activity, and it contains a button that calls startActivity() to display B. In short:
A -> B
A has a thread that ticks every minute. On this tick, a notification is updated in the status bar
This is what I am after:
1) When the user clicks the notification when neither A nor B is visible, show A 2) When the user clicks the notification when A is visible, nothing should happen 3) When the user clicks the notification when B is visible, nothing should happen (or, if it is easier, pop B to show A)
1) and 2) are easy to accomplish. 3) will start a new version of A and add it to the queue:
A -> B -> A, which is not what I intended. The user has to press back three times to go to the home screen
Is there a way to ensur开发者_开发技巧e that clicking the notification will either show the stack A -> B or A alone?
You should set the flag of activity A to singleTask and catch the new startActivity()
intent to the onNewIntent(Intent i)
of activity A (which you should override). Hope this helps!
Set FLAG_ACTIVITY_CLEAR_TOP
flag in startActivity()
method.
After that, if you want to recreate activity A, set FLAG_ACTIVITY_SINGLE_TOP
flag.
Or if you want to reuse activity A, set FLAG_ACTIVITY_SINGLE_TOP
flag.
精彩评论