无法从通知启动的活动中删除其他内容

问题描述

我有MainActivity,它是从通知中启动的,具有以下额外功能:

 val intent = Intent(this,MainActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.putExtra(Constants.EXTRA_NEWS_ID,id.toInt())
    val options = Bundle()
    options.putInt(Constants.EXTRA_NEWS_ID,id.toInt())
    val pendingIntent = PendingIntent.getActivity(
        this,id.toInt(),intent,PendingIntent.FLAG_ONE_SHOT,options
    )

然后在MainActivity onCreate中检查附加功能,并尝试删除附加功能:

if (intent.hasExtra(Constants.EXTRA_NEWS_ID)) {
        intentFromNotification = true
        Application.newsDetailId = intent.getIntExtra(Constants.EXTRA_NEWS_ID,0)
        Log.d("NOTIF","NewsId = ${NzmApplication.newsDetailId}")
        intent.removeExtra(Constants.EXTRA_NEWS_ID)
        intent.replaceExtras(Bundle())
        intent.action = ""
        intent.data = null
        intent.flags = 0
    }

稍后经过一些初始逻辑后,我打开了第二个ContentActivity,如下所示:

if (intentFromNotification) { // Activity called from notification = automaticly redirect to news detail
                        val intent = Intent(this,ContentActivity::class.java)
                        intent.putExtra(
                            Constants.EXTRA_FRAGMENT,Constants.FRAGMENT_NEWS_DETAIL
                        )
                        intentFromNotification = false
                        startActivity(intent)
                    }

ContentActivity可以正常工作,显示所需的内容,但是当我按ContentActivity时,应打开MainActivity并等待用户交互,但是它再次执行相同的代码块,如第一次打开并自动打开ContentActivity。仍然存在其他功能,因此应用程序基本上是循环的。

更奇怪的是,并非在所有设备上都会发生。 我有Sony Xperia XZ1 Compact(Android 9),但大约250位用户中没有其他人没有报告过此问题。

编辑: ContentActivity中的onBackPress

 override fun onBackPressed() {
    super.onBackPressed()
    if (supportFragmentManager.backStackEntryCount == 0) {
        this.finish()
    }
}

解决方法

问题出在开发人员设置中,“不保留活动” 经过一些特定的测试,我忘记禁用此设置 也许有人会觉得这很有帮助:)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...