I am writing a test that launches my main activity and, right after that, I put it into background by launching the home screen by using the following intent:
Intent intent= new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
After that, I try to put my app's activity back into foreground by using an intent similar to the one above (it doesn't have the CATEGORY_HOME flag and, instead, I am adding the activity's name and package). Since I need to make sure my app's activity was successfully launched, I am using the "startActivitySync" method from Instrumentation.
When I run the test I see that the app is being successfully put into background and then back to foreground, but the test run never finishes. It hangs forever in the "startActivitySync" method. Any ideas of why this is happening?
In my experience:
public testOne(){
MyActivity first = startActivitySync(...);
first.finish();
MyActivity second = startActivitySync(...);
}
public testTwo(){
MyActivity first = startActivitySync(...);
...
}
, testOne() will succeed,
but testTwo() will hang on "startActivitySync".
Suggested fix:
Cleanup your started activities at the end of every test, ex:
public testOne(){
MyActivity first = startActivitySync(...);
first.finish();
MyActivity second = startActivitySync(...);
second.finish();
}
public testTwo(){
MyActivity first = startActivitySync(...);
first.finish();
}
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