Testing that an action has been started with FLAG_ACTIVITY_CLEAR_TOP

Robolectric allows you to test that an Activity been started using shadowOf(activity).peekNextStartedActivity() . However, this method does not work if the Activity started using FLAG_ACTIVITY_CLEAR_TOP . This is intuitive, since the purpose of this flag is not to launch a new Activity , but to move the existing Activity to the back stack forward. Is there any way to test this scenario?

UPDATE

My testing scenario is as follows:

There are 3 actions, call them A, B and C. The activity under testing is B, which was launched A. B now starts C for the result, and when the result is received, it returns to A using FLAG_ACTIVITY_CLEAR_TOP . Although there was no activity A on the stack at the time, I expect it to be up and running through peekNextStartedActivity() .

+5
source share
3 answers

whenever you send an intent from an activity (for example), 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); 

in the above activity (SomeActivity in the example) you can use the getIntent method:

 getIntent().getFlags() 

so the real question is: how to break flags into their basic components (bitwise OR)

based on this article: http://code.tutsplus.com/articles/understanding-bitwise-operators--active-11301

just check the flags with the right component

 if ((getIntent().getFlags() & FLAG_ACTIVITY_CLEAR_TOP) != 0) { // do something here } 
+2
source

I tried the test:

 private void checkMainActivityIsStarted() { activity.showMainActivity(); Intent intent = shadowOf( activity ).getNextStartedActivity(); assertThat( intent ).hasComponent( Robolectric.application, MainActivity.class ); assertThat( intent.getFlags() ).isEqualTo( Intent.FLAG_ACTIVITY_CLEAR_TOP ); } 

For the following code:

 public void showMainActivity() { Intent intent = new Intent( this, MainActivity.class ); intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP ); startActivity( intent ); } 

Does it work for you?

UPDATE

For me, you should split this test into two:

  • Activity C returns the correct result code
  • Action B, when you call onActivityResult (this is a public method), activates activity A

For me, it makes no sense to test Android, passing intentions between actions. Correct me if I misunderstood something.

0
source

It definitely solves your problem.

  Intent i = new Intent(MyActivity.this, SomeActivity.class); i.setFlags(FLAG_ACTIVITY_CLEAR_TOP | SOME_OTHER_FLAGS...); startActivity(i); 

but if you can finish your current activity during intentions from B to C, and when he shows the result and finishes C, he def will select you on the screen.

0
source

Source: https://habr.com/ru/post/1207650/


All Articles