how can you tell when an Android activity is finished loading?

不打扰是莪最后的温柔 提交于 2019-11-30 08:17:21
Christopher Orr

As I mentioned in a comment, your view hierarchy should be working after your call to setContentView() early in onCreate(). I've never had any problems like this with any activity or test class..

I'm not sure this is of any help for this specific case, but in general you can determine when the UI event queue is empty by calling getInstrumentation().waitForIdleSync(). That'll block until there's no more UI events to process.

If you create a setUp() method like this in your test case extending ActivityInstrumentationTestCase2<MyActivity>

@Override
protected void setUp() throws Exception {
    super.setUp();

    final MyActivity activity = getActivity();

    tv1 = (EditNumber)activity.findViewById(resId1);
    tv2 = (EditNumber)activity.findViewById(resId2);
}

your Activity will be fully operational and the layout loaded, demonstrated in this case by the fact that you can access the Views and its content

@SmallTest
public void testSimpleCreate() {
    final MyActivity activity = getActivity();
    assertNotNull(activity);

    assertNotNull(tv1);
    assertEquals("mystr1", tv1.getText().toString());
    assertNotNull(tv1);
    assertEquals("mystr2", tv2.getText().toString());
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!