希望有人能帮我弄清楚,如果不是解决方案,至少是一个行为的解释。

存在的问题:

在某些设备上,按下启动器图标会导致当前任务被恢复,在其他设备上则会导致初始启动意图被触发(有效地重新启动应用程序)。为什么会发生这种情况?

细节:

当你按下“启动器图标”时,应用程序正常启动——也就是说,我假设,一个Intent以你的第一个Activity的名称启动,动作为android.intent.action.MAIN,类别为android.intent.category.LAUNCHER。但情况并非总是如此:

在大多数设备上,如果你在应用程序已经运行后按下启动器图标,该进程中当前运行的Activity将恢复(而不是初始Activity)。它以相同的方式恢复,就像你从操作系统菜单中的“最近任务”中选择它一样。这是我想在所有设备上的行为。

然而,在选定的其他设备上,会发生不同的行为:

On the Motorola Xoom, when you press the launcher icon, the App will always start the initial launch Activity regardless of what is currently running. I assume that the launcher icons always start the "LAUNCHER" intent. On the Samsung Tab 2, when you press the launcher icon, if you have just installed the app, it will always launch the initial Activity (Same as the Xoom) - however, after you restart the device after the install, the launcher icon will instead resume the app. I assume that these devices add "installed apps" into a lookup table on device startup which allow the launcher icons to correctly resume running tasks?

我读过很多答案,听起来类似于我的问题,但简单地添加android:alwaysRetainTaskState="true"或使用launchMode="singleTop"的活动不是答案。

编辑:

在这个应用程序最近一次启动后,我们发现在第一次重启后,这种行为已经开始在所有设备上发生。这对我来说似乎很疯狂,但通过重新启动过程,我实际上找不到哪里出了问题。


当前回答

我也遇到过同样的问题,原因是:

(Kotlin代码,在MainActivity中)

override fun onBackPressed() {
    finish()
}

所以当导航到我的MainActivity从我的LoginActivity我使用这个:

    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)

当使用这些标志时,我不需要在MainActivity中有一个onBackPressed(),它将在返回单击时自然退出应用程序。当按下Home键并返回应用程序时,它不会重新启动。

其他回答

这个解决方案对我很有效:

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent startMain = new Intent(Intent.ACTION_MAIN);
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
            return false;
        }
        else
            return super.onKeyUp(keyCode, event);
    }

信贷: 我需要最小化android应用程序的后退按钮点击

可能不能在所有设备上工作,但当返回按钮被按下时,会成功地创建home键行为,从而停止活动而不是结束活动。

在我的Cat s60上,我在开发者选项中启用了“不要保留活动”,再次禁用这个选项可以让我在不失去应用程序状态的情况下切换应用程序……

我也遇到过同样的问题,原因是:

(Kotlin代码,在MainActivity中)

override fun onBackPressed() {
    finish()
}

所以当导航到我的MainActivity从我的LoginActivity我使用这个:

    val intent = Intent(this, MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    startActivity(intent)

当使用这些标志时,我不需要在MainActivity中有一个onBackPressed(),它将在返回单击时自然退出应用程序。当按下Home键并返回应用程序时,它不会重新启动。

解决方案的人谁不知道编程和遇到这个问题在他们的android手机。 这主要是由于android版本的升级(只是我的假设)。升级后,你所有的应用程序都优化为使用更少的电池。但是,这反过来会降低你的设备速度。

如何解决

Go to settings>>Apps>>apps settings(look for settings sign anywhere on the screen- it is different on different devices)>>battery optimization(or similar opti[enter image description here][1]on)>> move all apps to 'not optimised' state (have to do 1 by 1 manually -may be allow/disallow in some phones). Your launcher app need to be 'not optimised'(Zen UI launcher in my case - this is the culprit I guess- you could try optimising/Not optimizing and restarting different app if you have time). Now restart your phone. (no need to reset data/safe mode or any trouble)

现在试试多任务处理吧。:) 按下启动器图标现在应该会导致当前任务被恢复。:) 你的设备会变成不用担心电池,反正它会耗尽的。

我在三星设备上也遇到了同样的问题。经过一番搜索,这些答案没有一个对我有用。我发现在AndroidManifest.xml文件中,launchMode被设置为singleInstance (android:launchMode="singleInstance")。删除launchMode属性修复了我的问题。