I believe there is a limitation within the launch(Intent startActivityIntent)
method of ActivityScenario. It only wants the Activity to be RESUMED or DESTROYED and if it isn't within 4.5 seconds then it throws that error.
Within public static ActivityScenario launch(Intent startActivityIntent)
of Activity Scenario, check the logic scenario.waitForActivityToBecomeAnyOf(State.RESUMED, State.DESTROYED);
If you can create your own custom Activity Scenario and adjust this line of code to be something like scenario.waitForActivityToBecomeAnyOf(State.ON_PRE_CREATE, State.DESTROYED);
then it will theoretically work for you.
This is happening because the Lifecycle.State
of your Activity when launching the Fragment is not either of the two specific lifecycle states specified by ActivityScenario. Again, I believe this may be a limitation of the API and I am filing an issue under Android-Test Repo. https://github.com/android/android-test/issues/new
manpreet
Best Answer
2 years ago
I'm trying to use the new FragmentScenario APIs of the androidx testing libraries for local testing and instrumentation testing(androidTest). The api works fine in local environment but in instrumental testing, it gives error: java.lang.AssertionError: Activity never becomes requested state "[RESUMED, DESTROYED]" (last lifecycle transition = "PRE_ON_CREATE")"
Help me in instumental testing(androidTest)
Please check complete error detail: