<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Test1" android:launchMode="singleInstance"/>
<activity android:name=".Test2" android:launchMode="singleInstance"/>
1.MainActivity启动Test1 Test1启动Test2
2.Test2启动Test1,Test1再启动Test2
如此循环往复10次,
问:有多少个栈?里面有多少个Test1,Test2?
答:本app共三个栈,MainActivity一个,Test1一个,Test2一个,每一个栈里面都只有一个实例
---------------------------------------------------------------------------------------------------------------------------------------
从上面的例子可以看出,无论多少字,singleInstance都是只有一个实例
---
singleInstance 字面意思是单例,其实际含义基本和字面意思相同。
其使用场景是:假设程序中有一个活动是允许其他程序调用的,如果想使其他程序和这个程序共享这个活动的实例。则可以使用singleInstance.
例如:
呼叫来电界面:
(7.1源码)
<!-- Main in-call UI activity. This is never launched directly
from outside the phone app; instead, it's either launched by
the OutgoingCallBroadcaster (for outgoing calls), or as the
fullScreenIntent of a notification (for incoming calls.) -->
<activity android:name="com.android.incallui.InCallActivity"
android:theme="@style/Theme.InCallScreen"
android:label="@string/phoneAppLabel"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation|keyboardHidden"
android:exported="false"
android:screenOrientation="nosensor"
android:directBootAware="true"
android:resizeableActivity="true">
</activity>
闹铃提醒界面
(7.1源码)
<!-- This activity displays only the timers that have expired with only a reset button
present. This makes the activity appropriate for display above the lock screen so that
users have the limited ability to silence expired timers but nothing else. -->
<activity android:name=".timer.ExpiredTimersActivity"
android:excludeFromRecents="true"
android:theme="@style/ExpiredTimersActivityTheme"
android:launchMode="singleInstance"
android:showOnLockScreen="true"
android:taskAffinity=""
android:configChanges="screenSize|keyboardHidden|keyboard|navigation"/>