Starting multiple instances of the same Activity from Service
I want to start multiple instance of the same Activity
class from a Service
. The reason I'm doing this, is because I have a Service
that runs a "scan" daily, and if it finds any malfunctions it should display a popup for each malfunction.
The Activity
that I'm starting is more like a Dialog
, has a Dialog theme
to display info about the malfunction.
Manfiest:
<activity
android:name=".ui.dialogs.MalfunctionActivity"
android:theme="@style/MyDialog"
android:launchMode="standard">
Intent to start the activity from Service
:
Intent displayMalf=new Intent(this, MalfunctionActivity.class);
displayMalf.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(displayMalf);
PROBLEM:开发者_StackOverflow社区 to start the Activity
from a Service
I need the FLAG_ACTIVITY_NEW_TASK
which somehow cancels the launchMode="standard"
from the manifest, and gives me just one Activity
even if I try to start multiple diffrent instances.
Is there anyway in which I can achieve this?
It was so simple. There is the 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.
Was exactly what I need. Thanks and sorry for answering on my question. It is not a habit. :)
Service will take the flag FLAG_ACTIVITY_NEW_TASK
to start the activity but here you can try like this:
Set the
instance of the handler
of the activity of which you want multiple instances, in the service.When you want the new instance of the activity use
handler.sendMessage(msg)
and on receiving this msg in your activity, start this activity again.
I guess your app works in the background and will display the popups even if the app is not in the foreground at the moment, right?
Otherwise I would use normal popup's (AlertViews) instead of starting new activities all the time.
If the app works in the background, you could tell the user with the first popup that your app has found one or more malfunctions and that he should activate the app for more details
精彩评论