为什么选择应用程序作为默认启动器会创建一个新活动而不破坏当前活动?

问题描述 投票:0回答:1

我在 Android 应用程序中遇到意外行为,选择该应用程序作为默认启动器会导致创建新的活动实例,而不会破坏当前的活动实例。

我的 MainActivity 根本不调用 onDestroy。

经过进一步调查,我注意到辅助类中的某些函数(在 MainActivity 中实例化)是通过这些辅助类的两个实例来调用的。这一观察结果与两个活动实例的发现相吻合。

我的 MainActivity 清单:

        <activity
            android:name=".presentation.MainActivity"
            android:clearTaskOnLaunch="true"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:exported="true"
            android:launchMode="singleTask"
            android:resizeableActivity="true"
            android:stateNotNeeded="true"
            android:supportsPictureInPicture="true"
            android:windowSoftInputMode="adjustPan">

            <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>

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

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

                <data android:scheme="metanode" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND_MULTIPLE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="*/*" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    ```
android kotlin default launcher
1个回答
0
投票

我假设您需要这些意图过滤器,但请检查

1.如果您使用的是 Android 12 及更高版本,按返回会将您的 Activity 置于后台而不是销毁它

2.您正在使用的意图标记,它们可以破坏任务中活动的现有实例

https://developer.android.com/guide/components/activities/tasks-and-back-stack https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks

© www.soinside.com 2019 - 2024. All rights reserved.