Google Play和Launcher推出了分开的活动

Sir*_*Lam 7 android

假设我有一个简单的应用程序,有a SplashScreenActivity和a MainActivity.
以下是我的一部分AndroidManifest.xml:

    <activity
        android:name=".front.activities.splashscreen.SplashScreenActivity"
        android:launchMode="standard"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".front.activities.main.MainActivity"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/Main_Title"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"/>
Run Code Online (Sandbox Code Playgroud)

SplashScreenActivity打开MainActivityonCreate().

问题

如果应用程序是从Google Play而不是启动器启动的,如果我按下主页并单击启动器中的应用程序图标,SplashScreenActivity则会再次启动一个应用程序图标,因此会MainActivity在后端堆栈上再启动一个应用程序.

重现步骤

  1. 如果应用程序打开,请将其杀死.
  2. 从Google Play打开应用.
  3. 按主页按钮
  4. 从启动器打开应用程序.您将知道SplashScreenActivity已经重新启动的通知(或者通过查看日志)
  5. 重复步骤3-4.每次重复时,再发一次SplashScreenActivityMainActivity启动.

经过几次试验,如果我们按下后退按钮,我们会注意到MainActivity后面的堆栈中有多个.

更多信息

  1. 如果应用程序不是从Google Play启动,而是在第一次从启动器启动(步骤2),则无法再现.
  2. 不仅是谷歌游戏,而且任何其他发送启动应用程序意图的应用程序都可以重现这一点.
  3. 如果我首先从启动器启动,然后从Google Play启动,SplashScreenActivity则会启动2 .但是,如果我再次从启动器按下应用程序图标,它将不会创建第3个SplashScreenActivity.它将首次推出MainActivity顶级产品.

我试过了什么

  1. SplashScreenActivityandroid:launchMode="singleTask".没有帮助.
  2. MainActivityandroid:launchMode="singleTask".这将防止MainActivity创建多个,但不能解决SplashScreenActivity多次启动的问题.您也可以假设MainActivity不应该设置为singleTask.

我的期望

通过点击启动器中的应用程序图标,Android应该能够在我的任务管理器中找到该应用程序,该应用程序已经启动并且只是将其置于顶部.

我怎样才能解决这个问题?

ata*_*nko 1

某些启动器存在此错误:当应用程序从主屏幕启动时,会创建初始活动的新实例,而不是恢复应用程序。可以通过添加来修复

if (!isTaskRoot()) {
    finish();
    return;
}
Run Code Online (Sandbox Code Playgroud)

onCreate()初始活动。也可以看看:

单击启动器图标时恢复上次活动

恢复顶级 Activity 而不是启动启动器 Activity