markdown [活动启动模式]了解活动的启动模式#android #android_tutorial #tutorial

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown [活动启动模式]了解活动的启动模式#android #android_tutorial #tutorial相关的知识,希望对你有一定的参考价值。

Since each Activity is made to work in different purpose. Some is designed to work separately with each Intent sent for example an Activity for email composing in email client. While some is designed to work as a singleton for example an email's inbox Activity. That's why it does matter to specify whether Activity is needed to be created a new one or to use the existed one. ```launchMode``` is designed for this specifically.

launchMode can be assigned directly as an attribute tag
```
<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask">
```

## There are 4 types of launch modes:

### standard
This is a default launch mode. Everytime a new activity instance will be created in the same task. A task is just a stack of activities.

### singleTop (single activity instance at the top)
It acts almost the same as standard one which means that singleTop Activity instance could be created as many as we want. Only difference is if there already is an Activity instance with the same type at the top of stack in the caller Task, there would not be any new Activity created, instead an Intent will be sent to an existed Activity instance through ```onNewIntent()``` method.

### singleTask (single activity instance in the whole task or stack)
An Activity with singleTask launchMode is allowed to have only one instance in the system (a.k.a. Singleton). If there is an existing activity in-between the task stack, **all of Activities placed above that singleTask Activity would be automatically and cruelly destroyed** in the proper way (lifecycle trigged) to make that an Activity we want to appear on top of stack. In the mean time, an Intent would be sent to the singleTask Activity through the lovely ```onNewIntent()``` method.
If you want to create the new task and instantiate the activity at the root of the new task. You need to set the ```taskAffinity``` to empty string, which means the activity has no affinity for any task, so everytime new task will be created. Read more about taskAffinity [here](https://developer.android.com/guide/topics/manifest/activity-element#aff)
```
<activity
    android:name=".SingleTaskActivity"
    android:label="singleTask launchMode"
    android:launchMode="singleTask"
    android:taskAffinity="">
```

### singleInstance
Its similar to singleTask, just that whenever singleInstance activity is instantiated, **new task will be created evertime**. Likewise, if another activity is called from singleInstance activity. There is some issue as you will see only one task in the task manager, just add ```taskAffinitity``` with empty string to enable mutliple tasks on task manager.

Ref:
- [Launch mode animated](https://itnext.io/the-android-launchmode-animated-cheatsheet-6657e5dd9b0f)
- [Android task, once and for all](https://android.jlelse.eu/https-medium-com-yashsoniweb-android-tasks-ffbd547ff5b8)


以上是关于markdown [活动启动模式]了解活动的启动模式#android #android_tutorial #tutorial的主要内容,如果未能解决你的问题,请参考以下文章

活动的生命周期活动的启动模式

Android-活动的启动模式

Android-活动的启动模式

[Android]活动的启动模式

安卓 活动的启动模式

Activity(活动)的启动模式