onNewIntent() called unnecessarily without extras

3

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. 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.

You can see (due to the object ID) that there is only a single instance of MyActivity involved here. The Activity is created, started and resumed as usual. There are no special flags in the original Intent (we log the flags in onCreate() and onNewIntent()).

Approximately 1 second after onResume() we see a call to onPause(), immediately followed by a call to onNewIntent() and then onResume(). The Intent passed to onNewIntent() contains flags 0x30000000 which is FLAG_ACTIVITY_NEW_TASK (0x10000000) and FLAG_ACTIVIY_SINGLE_TOP (0x20000000). The Intent passed to onNewIntent() has no extras, which cause the app to crash.

We have double-checked and there is absolutely no place in our code where we set both these flags in an 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". 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.

The Activity in question works correctly almost all the time, so there is no general problem with it. 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.

android
android-intent
android-lifecycle
asked on Stack Overflow Jan 14, 2020 by David Wasser

1 Answer

0

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. 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.

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.

I think this can happen for example if you do startIntent from button's onclicklisteners - fast clickers may be able to do it twice. Solution for that comes with RxJava debounce.

answered on Stack Overflow Feb 4, 2020 by Marian Paździoch

User contributions licensed under CC BY-SA 3.0