Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startActivitySync hangs when putting an activity back to the foreground

Tags:

android

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?

like image 483
Vicente Avatar asked Nov 14 '22 21:11

Vicente


1 Answers

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();
}
like image 74
user77115 Avatar answered Jan 08 '23 02:01

user77115