Robolectric allows testing that an Activity
has been started using shadowOf(activity).peekNextStartedActivity()
. However, this method doesn't seem to work if the Activity
is started with FLAG_ACTIVITY_CLEAR_TOP
. This is intuitive since the purpose of this flag is not to start a new Activity
but to bring an existing Activity
in back stack to front. Is there any way to test this scenario?
UPDATE
My testing scenario is the following:
There are 3 Activities involved, let's call them A, B and C. Activity under test is B, which was started by A. B now starts C for result, and when the result is received goes back to A using the FLAG_ACTIVITY_CLEAR_TOP
flag. Even though there is no Activity A in the stack at that time, I'm expecting it to be started and be available via peekNextStartedActivity()
.
whenever you send an intent from activity (for instance), you can use the set flags method:
Intent i = new Intent(MyActivity.this, SomeActivity.class);
i.setFlags(FLAG_ACTIVITY_CLEAR_TOP | SOME_OTHER_FLAGS...);
startActivity(i);
on the resulted activity (SomeActivity in the example) you can use getIntent method:
getIntent().getFlags()
so the real question is: how to split back the flags into their basic components (bitwise OR)
based on this article: http://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301
simply check the flags with the component you need
if ((getIntent().getFlags() & FLAG_ACTIVITY_CLEAR_TOP) != 0)
{
// do something here
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With