I want to start an activity from service context. But I am bound to use flag = Intent.FLAG_ACTIVITY_NEW_TASK which is creating multiple instance of the activity since it is throwing Run-Time-Exception with other flags. How can we launch an activity from service so that multi instances of activity will not be created, if already activity in recent tasks it will launch that only??
-
possible duplicate of [Start a Service from activity](http://stackoverflow.com/questions/2334955/start-a-service-from-activity) – Abhinav Singh Maurya Aug 03 '15 at 11:37
1 Answers
You can combine different flags, like FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.
...
This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are finished. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK.
with FLAG_ACTIVITY_NEW_TASK. Which one to use depends on what exactly do you want to achieve. Check the documentation of the flags in the Intent class and Tasks and Back Stack.