如何检查新 activity 是否已启动

How can I check if the new activity was started

场景:

  1. 用户启动应用程序
  2. 用户转到"CreateAccountActivity"
  3. 为了使用该应用程序,用户必须输入电子邮件和密码
  4. 用户点击 "create" 切换到下一个 activity。

    @Test
    public void createAccountAndLogIn() {
        onView(withId(R.id.login_create)).perform(click());
        onView(withId(R.id.create_email)).perform(clearText(),typeText("xxx@gmail.com"));
        onView(withId(R.id.create_pass)).perform(clearText(),typeText("pass"));
        onView(withId(R.id.create_pass_repeat)).perform(clearText(),typeText("pass"));
        onView(withId(R.id.create_create_button)).perform(click());
    }
    

如何在最后一行之后检查是否启动了新的 "MainActivity"?

You can control this defining a variable in Application class. When you define a variable in Application class , variable be protected until your app kill and you can reach this variable every class in project. I think it resolves your problem.

您是否尝试过在 MainActivity 中使用 onResume() 方法作为快速破解方法?有一个 boolean 变量,在 onResume() 中变为 true,在 onPause() 中变为 false

干杯, SSG

使用 ActivityLifecycleMonitor 是可能的...但是它不会是您正在寻找的 "black box" 方法。

如果你想要全黑盒,断言下一个视图 activity 应该足以通过测试。

这里是同一个问题,有更详细的解释。