[英]startActivitySync hangs when putting an activity back to the foreground
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). 之后,我尝试使用类似于上面的一个意图将我的应用程序的活动放回到前台(它没有CATEGORY_HOME标志,而是我添加了活动的名称和包)。 Since I need to make sure my app's activity was successfully launched, I am using the "startActivitySync" method from Instrumentation.
由于我需要确保我的应用程序的活动已成功启动,因此我使用了Instrumentation中的“startActivitySync”方法。
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.
它永远挂在“startActivitySync”方法中。 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, ,testOne()会成功,
but testTwo() will hang on "startActivitySync". 但testTwo()将挂起“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();
}
There're lots of such questions but almost all answerers suggest to finish an activity which I suppose in most cases does not work. 有很多这样的问题,但几乎所有的回答者都建议完成一项活动,我认为在大多数情况下都不起作用。
If your activity doesn't finish it just means that some UI operation continues to run. 如果您的活动未完成,则仅表示某些UI操作继续运行。 And actually it could mean that something wrong with your activity.
实际上,这可能意味着您的活动出现了问题。 For instance recently I faced similar problem and found out that an animation was not appropriately stopped:
例如,最近我遇到了类似的问题,发现动画没有被适当地停止:
Animation anim = AnimationUtils.loadAnimation(getContext(), R.anim.rotate_infinite_clockwise);
view.startAnimation(anim);
So the most straight forward method of solving the problem is disabling views one by one. 因此,解决问题最直接的方法是逐个禁用视图。
Instead of using this constructor: 而不是使用此构造函数:
Intent intent = new Intent(String action);
try this one: 试试这个:
Intent intent = new Intent(Context packageContext, Class<?> cls):
I ran into a similar issue with startActivitySync(Intent)
hanging, and this resolved it for me. 我在
startActivitySync(Intent)
挂起时遇到了类似的问题,这解决了我的问题。 It worked fine on the first test but failed on subsequent tests in the same class. 它在第一次测试时运行良好,但在同一级别的后续测试中失败。 Why?
为什么? I don't know.
我不知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.