简体   繁体   English

onNewIntent() 在没有额外的情况下不必要地调用

[英]onNewIntent() called unnecessarily without extras

We have crash reports from our App that we cannot explain.我们有无法解释的应用程序崩溃报告。 The crash occurs in MyActivity because an expected "extra" is missing from the Intent .崩溃发生在MyActivity因为Intent缺少预期的“额外”。 We have extensive logging in our app and this is the sequence of lifecycle callbacks that we see when this occurs:我们在我们的应用程序中有大量日志记录,这是发生这种情况时我们看到的生命周期回调序列:

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

We log the object ID (in this case 80773a0 ) so that we can tell how many instances of a given Activity are in use.我们记录对象 ID(在本例中为80773a0 ),以便我们可以知道给定Activity多少个实例正在使用中。

You can see (due to the object ID) that there is only a single instance of MyActivity involved here.您可以看到(由于对象 ID)这里只涉及MyActivity的一个实例。 The Activity is created, started and resumed as usual. Activity像往常一样被创建、启动和恢复。 There are no special flags in the original Intent (we log the flags in onCreate() and onNewIntent() ).原始Intent中没有特殊标志(我们在onCreate()onNewIntent()记录标志)。

Approximately 1 second after onResume() we see a call to onPause() , immediately followed by a call to onNewIntent() and then onResume() .onResume()大约 1 秒后,我们看到对onPause()的调用,紧接着是对onNewIntent()的调用,然后是onResume() The Intent passed to onNewIntent() contains flags 0x30000000 which is FLAG_ACTIVITY_NEW_TASK ( 0x10000000) and FLAG_ACTIVIY_SINGLE_TOP ( 0x20000000 ).传递给onNewIntent()Intent包含标志0x30000000 ,即FLAG_ACTIVITY_NEW_TASK ( 0x10000000)FLAG_ACTIVIY_SINGLE_TOP ( 0x20000000 )。 The Intent passed to onNewIntent() has no extras, which cause the app to crash.传递给onNewIntent()Intent没有额外的内容,这会导致应用程序崩溃。

We have double-checked and there is absolutely no place in our code where we set both these flags in an Intent .我们已经仔细检查过,在我们的代码中绝对没有地方在Intent设置这两个标志。

We do not understand what is causing the calls to onPause(), onNewIntent() and onResume() and we do not understand why the Intent has this set of flags nor why it does not contain the necessary "extras".我们不明白是什么导致对onPause(), onNewIntent()onResume()的调用,我们不明白为什么Intent有这组标志,也不明白为什么它不包含必要的“额外”。 There is only one place in the app where this Activity is launched and the necessary "extras" are put in the Intent before startActivity() is called.应用程序中只有一个地方可以启动此Activity ,并且在调用startActivity()之前将必要的“额外内容”放入Intent

The Activity in question works correctly almost all the time, so there is no general problem with it.有问题的Activity几乎一直都在正常工作,所以它没有普遍的问题。 There must be some specific behaviour that causes this problem and we have exhausted our ideas about what it might be.一定有一些特定的行为导致了这个问题,我们已经穷尽了关于它可能是什么的想法。

The manifest entry for MyActivity has no special launch mode specified. MyActivity的清单条目没有指定特殊的启动模式。

If possible then please extract parts of code necessary to examine this issue because we're shooting blind.如果可能,请提取检查此问题所需的部分代码,因为我们正在盲目射击。

What I can read in docs is:我可以在文档中阅读的是:

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.您可以指望在此方法之后调用 onResume(),但不一定在此回调完成后立即调用。 If the activity was resumed, it will be paused and new intent will be delivered, followed by onResume().如果活动被恢复,它将被暂停并传递新的意图,然后是 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.如果活动未处于恢复状态,则可以立即交付新的意图,稍后在活动再次变为活动状态时调用 onResume()。

So it seems that your case is exactly matched in that part of docs!因此,您的案例似乎与文档的那部分完全匹配! I assume some basterd is calling startActivity with flag FLAG_ACTIVITY_SINGLE_TOP twice in a row.我认为一些basterd呼吁startActivity与标志FLAG_ACTIVITY_SINGLE_TOP连续两次。

I think this can happen for example if you do startIntent from button's onclicklisteners - fast clickers may be able to do it twice.我认为,如果你这样做可能发生例如startIntent从按钮的onclicklisteners -快速的表决可能能够做两次。 Solution for that comes with RxJava debounce . RxJava debounce 提供了解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM