Android - use of FLAG_ACTIVITY_NEW_TASK
Solution 1
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.
Solution 2
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.
Cygnus
Updated on July 18, 2022Comments
-
Cygnus almost 2 years
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 ?
-
Cygnus about 11 yearsUmm i did not get it...when clicking the notification, the same instance that triggered my notification should appear. If i dont set any flag, a new instance is launched and i dont want that..
-
meredrica about 11 yearsTry using single top then. The thing you want to accomplish is actually quite tricky - I have code for this at home and can look it up later.
-
Cygnus about 11 yearsOk..but when we create the PendingIntent we specify the context in which to start the activity as the first argument to getActivity(). So shouldnt it be started according to that context ? Or am i missing something ?
-
Cygnus about 11 yearsAlso, if the docs say "if a task is already running for the activity you are now starting, then a new activity will not be started", then in this case isnt it the case that the task for the activity is running ?
-
Graeme about 11 yearsDo a bit of reading on
Task
s. Here is the Documentation on getActivity - developer.android.com/reference/android/app/…, int, android.content.Intent, int) - it says "Note that the activity will be started outside of the context of an existing activity" -
Graeme about 11 yearson one of the App's I use which use Notification i use getBroadcast() and have each of my Activities register Broadcast Receivers to handle calls from the Notification (and a default, backup receiver when there are no Activities to catch it).
-
Cygnus about 11 yearsThanks !! I find the docs to be quite confusing for some sections..basically for PendingIntent, we should use getApplicationContext() as the first argument right ?
-
Graeme about 11 yearsYou can use any
Context
you like, but from the documentation is seems it will ALWAYS be part of a new Task. -
dentex over 10 years@meredrica Would appreciate a lot some insights, to fix a similar problem. I ended using the manifest's
android:launchMode="singleTop"
, but it prevents the app to have a back-history when, i.e., I recycle the preference activity changing theme on-the-fly. Thanks. -
sakis kaliakoudas about 9 years@Graeme I used your suggestion with broadcast receivers and it works great for most phone but it seems like it doesn't work for some phones/android versions, I've not narrowed it down yet. Do you have any idea on what could cause this ? Specifically when the flag Intent.FLAG_ACTIVITY_NEW_TASK is included then it works for all phones but when it isn't then it only works for some phones, and clicking on the notifications doesn't do anything at all for some others.!
-
Graeme about 9 years@sakiskaliakoudas Try adding a random number parameter to your broadcast. Sometimes weird caching happens at the OS level which blocks your intents.
-
sakis kaliakoudas about 9 years@Graeme not sure I understand. The pending broadcast is delivered to my broadcast receiver, but after that when I call for the activity to start with FLAG_REORDER_TO_FRONT nothing happens. I've only seen this in android 4.4.2 devices
-
Graeme about 9 yearsin you Broadcast intent, try adding putExtra("RandomNumber", Math.rand() * 100000); I had an issue with some versions of Android thinking a Broadcast was a duplicate and so discarded it. Might not be the same thing but worth trying.