Starting Activity through notification: Avoiding duplicate activities

后端 未结 3 774
小鲜肉
小鲜肉 2020-11-30 21:41

So I am currently showing a notification. When the user clicks this noticiation, the application is started. The notification persists, to indicate that the service is runni

相关标签:
3条回答
  • 2020-11-30 22:00

    As the two answers above have mentioned, you'll want to set the application's launch mode which is defined in the activity's definition in the manifest:

    <activity
        android:name="com.company.ActivityName"
        android:launchMode="singleTask">
    </activity>
    

    Additionally, you may want to note, that despite FLAG_ACTIVITY_SINGLE_TOP being a valid Intent flag, there are no equivalent intent flags for singleTask or singleInstance.

    See the launchMode section for more details on the different launch mode options: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

    0 讨论(0)
  • 2020-11-30 22:03

    That's the way it's supposed to be by default. You probably need to specify android:launchMode="singleTop" if you want to have a single instance only.
    There are 4 launch modes, more info here: https://developer.android.com/guide/topics/manifest/activity-element.html

    0 讨论(0)
  • 2020-11-30 22:22

    When using the lanchMode="singleTask", if an instance of your activity already exists, Android does not re-create the Activity but launch it with the onNewIntent() method.

    As documented by Android :

    The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

    Android documentation for activity mode

    0 讨论(0)
提交回复
热议问题