Sahil Rally
Sahil Rally

Reputation: 21

Multiple Instances of an Activity

I am trying to show the user information on incoming-call screen, whenever there is an incoming-call. So I have a broadcast receiver listening to incoming calls, which starts the intent service, which subsequently starts an activity (with Theme Dialog).

Now, whenever there is an incoming-call, my activity dialog pops up and shows as intended.

Problem: When the activity dialog is already on the screen and incoming-call happens, there is no new activity dialog with new information. I guess that whenever there is an instance, Android does not creates the new one. So it seems like my problem is "creating multiple instances of an activity".

Please note that I am starting an activity from an intent service using FLAG_NEW_TASK.

Upvotes: 2

Views: 1431

Answers (3)

Mehul Joisar
Mehul Joisar

Reputation: 15358

Simply add following flags to your Intent .

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
            | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
            | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

Upvotes: 2

neeraj
neeraj

Reputation: 477

Use flag FLAG_ACTIVITY_MULTIPLE_TASK which according to the documentation :

Used in conjunction with FLAG_ACTIVITY_NEW_TASK to disable the behavior of bringing an existing task to the foreground. When set, a new task is always started to host the Activity for the Intent, regardless of whether there is already an existing task running the same thing.

Using this flag along with FLAG_ACTIVITY_NEW_TASK will cause each activity instance to be created as a separate task and thus you can have different dialog pop ups.

Upvotes: 2

Moin Ahmed
Moin Ahmed

Reputation: 2898

Google Doc says :

FLAG_ACTIVITY_NEW_TASK

"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."

So, if you want to start a new fresh activity then simply not use this flag only, you should use it with FLAG_ACTIVITY_CLEAR_TASK for the desired result.

For Example:

// Sets the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);

If the above solution is not what you needed, then have a look at android:launchMode attribute, declare this attribute with the desired options (i.e. as per your need) in activity tag of manifest file.

Hope this will solve the problem.

Upvotes: 3

Related Questions