Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - use of FLAG_ACTIVITY_NEW_TASK

I have created a simple application having a button. Clicking it triggers a notification, and clicking on the notification launches a new instance of the same application. However, I wanted that clicking on the notification should bring me back to the application instance from which the notification was triggered. For this I consulted the Android docs for the FLAG_ACTIVITY_NEW_TASK flag-

When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK for a flag to disable this behavior.

Based on this when creating the intent for passing to the PendingIntent, i set this flag. However, clicking on the notification still launches a new instance of the application.

What am I doing wrong ?

like image 691
Cygnus Avatar asked Apr 08 '13 14:04

Cygnus


2 Answers

Remember that when you click the Notification it is from that Context that the intent is being launched. That context doesn't have the Activity on it's task (infact, it will be a blank task).

What this results in is two version of the same Activity (although still only one instance of you Application) running. Each Activity is running a different Task.

If you don't need duplicate Activities of the same type in any of your stacks you could use the answer here:

https://stackoverflow.com/a/2327027/726954

Otherwise, there are many ways to "fix" this problem, including singleton variables and Application Context methods that keeps track of which Activities are in a Running state.

You may need to search and refine your question for those.

like image 139
Graeme Avatar answered Sep 22 '22 00:09

Graeme


A Task in Android is a separate User workflow. If you mange to see the Homescreen sometime, that usually means you start a new one. Remove the flag and it should work. if it does not, try using Single top.

like image 35
meredrica Avatar answered Sep 20 '22 00:09

meredrica