Atif Farrukh
Atif Farrukh

Reputation: 2261

Start Multiple Instances of an activity from BroadcastReceiver

I want to create multiple instances of an activity from BroadcastReceiver, the activity contains a AlertDialog, currently I am using the following code for this purpose:

 Intent intent = new Intent(this, MultipleInstanceActivity.calss);
 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 context.startActivity(intent);

and in manifest file android:launchMode="standard" somehow I think this FLAG_ACTIVITY_NEW_TASK causing the android:launchMode="standard" to change to android:launchMode="singleInstance" or something. I am not able to create multiple instances of this activity. I also tried to use FLAG_ACTIVITY_MULTIPLE_TASK, but no use.

I have created a PreferenceActivity, what really puzzles me is that when this PreferenceActivity is open my app creates multiple dialogbox i.e multiple instances with different data on it. But when its not open, my app wont create multiple instances just to make clear, it wont open another dialog. Logcat is not giving any warnings or errors.

My questions:

  1. How to create multiple instances of an activity from BroadcastReceiver?

  2. Can someone explain me whats happening in the second case, the PreferenceActivity one, why is it creating multiple instances?

Upvotes: 0

Views: 764

Answers (1)

Chris.Zou
Chris.Zou

Reputation: 4576

I ran into the same problem as you have here, and i solved it by Using both FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.

Intent intent = new Intent(context, YourActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);

Hope this works for you too.

Upvotes: 2

Related Questions