Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm in the process of working on an automated test suite for our android app, and running into trouble waiting for activities to fully load. I can call getActivity, but just because it shows the activity that I'm hoping to see in my test doesn't always seem to mean that the activity's components are ready for use (fully loaded). Looking through the Activity API didn't turn anything up, and other methods seem too invasive and have spoiled the tests initial state. Does anyone know if there's a way to ask the app or the VM if the current activity is loaded?

like image 503
Derrick Avatar asked Jan 18 '10 21:01

Derrick


People also ask

How do I know if my activity is finished?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.

How do I know if my android activity is recreated?

You can determine if the activity is finishing by user choice (user chooses to exit by pressing back for example) using isFinishing() in onDestroy . @Override protected void onDestroy() { super. onDestroy(); if (isFinishing()) { // wrap stuff up } else { //It's an orientation change. } }

What does Android activity mean?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.


2 Answers

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.

like image 107
Christopher Orr Avatar answered Oct 13 '22 22:10

Christopher Orr


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());
}
like image 21
Diego Torres Milano Avatar answered Oct 13 '22 22:10

Diego Torres Milano