singleTask 启动模式不起作用
singleTask launch mode is not working
即使在将 launchMode 指定为 singleTask 之后,我的 activity 的多个实例也会被创建,这很奇怪。有人遇到过这个问题吗?任何指针将不胜感激。
下面是我在清单中声明我的 activity 的方式:
<activity
android:name="com.test.TestActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
这就是我在 activity 仍在前台时启动它的方式:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
sendIntent.setType("text/plain");
activity.startActivity(Intent.createChooser(sendIntent, "Share"));
Android 我测试的版本是 5.1.1
如果您想摆脱 activity 的多个实例,请尝试使用 singleInstance。
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
额外说明:
singleTask :- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity
instance exists in any tasks, the system routes the intent to that
activity instance through the onNewIntent() method call. In this mode,
activity instances can be pushed to the same task. This mode is useful
for activities that act as the entry points.
singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s.
Accordingly, the activity with launch mode is always in a single
activity instance task. This is a very specialized mode and should
only be used in applications that are implemented entirely as one
activity.
Copied from :
看来问题出在设备上。在模拟器和其他具有相同 android 版本的设备上,singleTask 工作正常。
即使在将 launchMode 指定为 singleTask 之后,我的 activity 的多个实例也会被创建,这很奇怪。有人遇到过这个问题吗?任何指针将不胜感激。
下面是我在清单中声明我的 activity 的方式:
<activity
android:name="com.test.TestActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
这就是我在 activity 仍在前台时启动它的方式:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
sendIntent.setType("text/plain");
activity.startActivity(Intent.createChooser(sendIntent, "Share"));
Android 我测试的版本是 5.1.1
如果您想摆脱 activity 的多个实例,请尝试使用 singleInstance。
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
额外说明:
singleTask :- A new task will always be created and a new instance will be pushed to the task as the root. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. This mode is useful for activities that act as the entry points.
singleInstance:- Same as singleTask, except that the no activities instance can be pushed into the same task of the singleInstance’s. Accordingly, the activity with launch mode is always in a single activity instance task. This is a very specialized mode and should only be used in applications that are implemented entirely as one activity.
Copied from :
看来问题出在设备上。在模拟器和其他具有相同 android 版本的设备上,singleTask 工作正常。