是什么让singleTask活动有2个实例?

Tam*_*ári 7 android android-activity android-task

根据文档,singleTask活动不能有多个实例.我的应用程序的唯一活动是singleTask,它同时有2个实例.

重新创建问题的步骤

步骤1

在Android Studio 3.3.1中创建一个新项目,Add No Activity,将其命名为singleTaskBug,(package com.example.singletaskbug),使用最低API级别为21的Java语言,而不支持即时应用程序.

第2步

通过编辑手动添加一个新的活动AndroidManifest.xml,然后创建一个新的Java类appjavacom.example.singletaskbug命名LauncherActivity.

内容AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LauncherActivity"
        android:excludeFromRecents="true"
        android:launchMode="singleTask">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />

        </intent-filter>

    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

内容LauncherActivity.java:

package com.example.singletaskbug;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;

public class LauncherActivity extends Activity {

    static int instanceCounter = 0;
    final int instanceId;
    final String TAG = "STB";

    public LauncherActivity() {
        instanceId = ++instanceCounter;
        Log.d(TAG, "Constructed instance " + instanceId + ".");
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "Created instance " + instanceId + ".");
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "New intent to instance " + instanceId + ".");
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "Destroyed instance " + instanceId + ".");
        super.onDestroy();
    }
}
Run Code Online (Sandbox Code Playgroud)

第3步

转到RunEdit Configurations...,并在Launch Options设置部分Launch:Specified Activity,和Activity: com.example.singletaskbug.LauncherActivity,然后单击OKRun 'app' ShiftF10.

第4步

等到活动变得可见.现在在测试设备上(在我的情况下为API 21),转到设置将此应用程序设置为默认启动器.然后按主页按钮.此时你会在Logcat中看到这个:

02-15 17:22:01.906 26429-26429/com.example.singletaskbug D/STB: Constructed instance 1.
02-15 17:22:01.916 26429-26429/com.example.singletaskbug D/STB: Created instance 1.
02-15 17:22:24.228 26429-26429/com.example.singletaskbug D/STB: Constructed instance 2.
02-15 17:22:24.248 26429-26429/com.example.singletaskbug D/STB: Created instance 2.
Run Code Online (Sandbox Code Playgroud)

And*_*oom 0

一个 Android 应用程序可以有多个任务。每个任务可以有多个活动。singleTasksingleInstance控制任务内部活动的行为(其唯一性),但应用程序可能有两个或多个Activity内部相同的任务。

这就是这里实际看到的,一个有两个任务的应用程序LauncherActivity,每个任务都有一个内部任务。作为解决方法,请确保应用程序中始终有一项任务。在LauncherActivity onCreate添加:

val appTasks = getSystemService(ActivityManager::class.java).appTasks
if (appTasks.size >= 2) {
    appTasks.dropLast(1).forEach { it.finishAndRemoveTask() }
    appTasks.last().moveToFront()
    return
}
Run Code Online (Sandbox Code Playgroud)