使用 Espresso,我尝试测试使用 Home 按钮将 Activity 发送到后台,然后再次将其放在前台进行一些检查:
@EspressoTest
public void test() {
onSomeView().check(matches(isDisplayed()));
getInstrumentation().sendKeyDownUpSync(KeyEvent.KEYCODE_HOME);
Context context = getInstrumentation().getTargetContext();
Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
onSomeView().check(matches(isDisplayed()));
}
我不得不使用intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
这是由异常建议的,但除此之外我还测试了,将其作为启动器活动启动,或使用FLAG_ACTIVITY_REORDER_TO_FRONT
,但视图不可见。尽管测试通过了。
Please consider this response as it works 100% when you want to
goBackgroundByClickingHomeThenGetBackToTheApp.
UiDevice device = UiDevice.getInstance(getInstrumentation());
device.pressHome();
device.pressRecentApps();
device.findObject(new UiSelector().text(getTargetContext().getString(getTargetContext().getApplicationInfo().labelRes)))
.click();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)