是什么让 singleTask 活动有 2 个实例? [英] What makes a singleTask activity have 2 instances?

查看:141
本文介绍了是什么让 singleTask 活动有 2 个实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As per the docs, singleTask activities can't have multiple instances. The only activity of my app is singleTask, and it has 2 instances at the same time.

在Android Studio 3.3.1中新建一个项目,Add No Activity,命名为singleTaskBug,(包com.example.singletaskbug),使用Java语言,最低API级别21,不支持instant应用.

Create a new project in Android Studio 3.3.1, Add No Activity, name it singleTaskBug, (package com.example.singletaskbug), using Java language with minimum API level 21 without support for instant apps.

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

Add a new activity manually by editing AndroidManifest.xml then creating a new Java Class in appjavacom.example.singletaskbug named 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>

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();
    }
}

步骤 3

转到 RunEdit Configurations... 并在 Launch Options 部分将 Launch: 设置为Specified ActivityActivity: com.example.singletaskbug.LauncherActivity,然后点击OK运行'app' ShiftF10.

Step 3

Go to RunEdit Configurations... and in the Launch Options section set Launch: to Specified Activity, and Activity: com.example.singletaskbug.LauncherActivity, then click OK, and Run 'app' ShiftF10.

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

Wait until the activity becomes visible. Now on the test device (API 21 in my case), go to settings to set this app as the default launcher. Then press the home button. At this point you'll see this in 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.

推荐答案

一个 Android 应用程序可以有多个任务.每个任务可以有多个活动.singleTasksingleInstance 控制任务内 Activity 的行为(其唯一性),但可能会发生一个应用程序具有两个或多个具有相同 Activity 的任务 在里面.

An Android application can have multiple tasks. Each task can have multiple activities. singleTask and singleInstance control the behaviour of the activity (its uniqueness) inside the task, but it can happen that an App has two or more tasks with the same Activity inside.

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

This is what is actually seen here, an App with two tasks with an LauncherActivity inside, one for each tasks. As workaround, ensure that there is always one task in the app. On the LauncherActivity onCreate add:

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

这篇关于是什么让 singleTask 活动有 2 个实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆