android - onNewIntent() 不必要地调用,没有额外的内容

标签 android android-intent android-lifecycle

我们的应用程序收到了我们无法解释的崩溃报告。 MyActivity 中发生崩溃,因为 Intent 中缺少预期的“额外”。我们的应用程序中有大量日志记录,这是发生这种情况时我们看到的生命周期回调序列:

06:04:22.574#a.b.c.MyActivity.onCreate() with flags 0 a.b.c.MyActivity@80773a0
06:04:22.592#a.b.c.MyActivity.onStart() a.b.c.MyActivity@80773a0
06:04:22.596#a.b.c.MyActivity.onResume() a.b.c.MyActivity@80773a0
06:04:23.601#a.b.c.MyActivity.onPause() a.b.c.MyActivity@80773a0
06:04:23.614#a.b.c.MyActivity.onNewIntent() with flags 30000000 a.b.c.MyActivity@80773a0
06:04:23.654#a.b.c.MyActivity.onResume() a.b.c.MyActivity@80773a0

我们记录对象 ID(在本例中为 80773a0),以便了解给定 Activity 有多少个实例正在使用。

您可以看到(由于对象 ID)这里只涉及一个 MyActivity 实例。 Activity 照常创建、启动和恢复。原始 Intent 中没有特殊标志(我们在 onCreate()onNewIntent() 中记录标志)。

onResume()之后大约1秒,我们看到对onPause()的调用,紧接着对onNewIntent()的调用,并且然后onResume()。传递给 onNewIntent()Intent 包含标志 0x30000000,即 FLAG_ACTIVITY_NEW_TASK (0x10000000)FLAG_ACTIVIY_SINGLE_TOP (0x20000000)。传递给 onNewIntent()Intent 没有额外的内容,这会导致应用崩溃。

我们已经仔细检查过,我们的代码中绝对没有任何地方可以在 Intent 中设置这两个标志。

我们不明白是什么导致调用 onPause()、onNewIntent()onResume() 并且我们不明白为什么 Intent 有这组标志,也解释为什么它不包含必要的“额外内容”。应用程序中只有一个位置可以启动此 Activity,并且在 startActivity() 之前将必要的“额外内容”放入 Intent 中已调用。

相关的Activity几乎一直都能正常工作,因此不存在一般性问题。一定有一些特定的行为导致了这个问题,我们已经用尽了我们对可能是什么的想法。

MyActivity 的 list 条目没有指定特殊的启动模式。

最佳答案

如果可能,请提取检查此问题所需的部分代码,因为我们是盲目的。

我可以在docs中读到什么是:

An activity can never receive a new intent in the resumed state. You can count on onResume() being called after this method, though not necessarily immediately after the completion this callback. If the activity was resumed, it will be paused and new intent will be delivered, followed by onResume(). If the activity wasn't in the resumed state, then new intent can be delivered immediately, with onResume() called sometime later when activity becomes active again.

看来您的案例与文档的该部分完全匹配! 我假设某个 SCSS 连续两次使用标志 FLAG_ACTIVITY_SINGLE_TOP 调用 startActivity

我认为,例如,如果您从按钮的 onclicklisteners 执行 startIntent ,就会发生这种情况 - 快速点击者可能能够执行两次。解决方案附带 RxJava debounce .

关于android - onNewIntent() 不必要地调用,没有额外的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59738069/

相关文章:

android - 如何在android中单击按钮更新小部件的 View ?

android - 窗口已经聚焦 - 忽略 com.android.internal.view 的聚焦增益

android - 通过 Android Intent 在 Google map 中添加多个航路点

android - BroadCast Receiver 正在计算电源按钮的点击次数,但无法恢复最小化的 Activity

android - 使用 LifecycleObserver 的生命周期感知组件如何感知屏幕方向变化

android - 关于其 Activity 的 fragment 生命周期

android - Android 是如何实现对 SuperNotCalledException 的检查的?

java - Android Studio 无法读取文件,该文件不存在

android - 当 Activity B 更改公共(public)文本后,如何在 Activity A 中显示公共(public)文本

android - 文档中文档的 Firebase Firestore 子集合