Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing that an Activity has been started with FLAG_ACTIVITY_CLEAR_TOP

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

like image 622
Egor Avatar asked Nov 25 '14 09:11

Egor


1 Answers

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
}
like image 60
ymz Avatar answered Oct 07 '22 00:10

ymz