ActivityInstrumentationTestCase2 is the right approach as another class is deprecated. To check what happened after your main action, call it ProgressActivity , you should use ActivityMonitor . I think you want to intercept the creation of the Activity , not the destruction.
I assume that ProgressActivity starts another Activity (say A1 , A2 or A3 ) after some calculations are done in the background.
Your test case should look something like this:
public static final HashSet<Class<? extends Activity>> TARGET_ACTIVITIES = new HashSet<Class<? extends Activity>>(); static { TARGET_ACTIVITIES.add(A1.class); TARGET_ACTIVITIES.add(A2.class); TARGET_ACTIVITIES.add(A3.class); } private static final int TIMEOUT = 7000; public void testRandomActivityStarted() { @SuppressWarnings("unused") ProgressActivity activity = getActivity(); final Instrumentation inst = getInstrumentation(); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MAIN); intentFilter.addCategory("MY_CATEGORY"); ActivityMonitor monitor = inst.addMonitor(intentFilter, null, false);
The trick here is to use a category in IntentFilter , because if you rely on getLastActivity() , you may be disappointed, as it seems to always be zero. To fit this category, you must use it when starting A1 , A2 or A3 ( Intent.addCatrgory () )
This example has been adapted from the one that illustrates ActivityMonitor in the Android Application Test Guide .
source share