Check if the action is running in the service

I have a service that does some background work, then I need to start the Activity, showing some results that the Service processes. But there is a possibility that the activity is launched many times from the service. Now I want to start this operation only if it is already inactive.

What is the opportunity and how to do it? And the sample code will be nice if you don't mind.

Thank!!

+5
source share
1 answer

You must change launchModeyour activity to singleTaskin the androidManifest.xml file.

The default value for this property is equal standard, which allows you to run any number of instances.

"singleTask" "singleInstance" . . , - . [...]

"singleTask" "singleInstance" : "singleTask" . , ( "" "" ). , "singleInstance" . . , - FLAG_ACTIVITY_NEW_TASK .

Android Developers 'Guide ( )

<activity android:name=".activity.YourActivity" 
    android:launchMode="singleTask"
    android:alwaysRetainTaskState="true"
    android:clearTaskOnLaunch="false" 
    android:finishOnTaskLaunch="false">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
+2

All Articles