First, thanks for this tool! IdlingResource is a total mess.
Now to business, from the documentation I was not able to understand how to get the latest activity.
However, I found a different way, which follows:
//ref: http://stackoverflow.com/a/38990078/689223
static private Activity getCurrentActivity(){
Collection<Activity> resumedActivity = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
for(Activity act : resumedActivity){
return act;
}
return null;
}
static private Activity getCurrentActivitySafe(){
Callable<Activity> callable = new Callable<Activity>() {
@Override
public Activity call() throws Exception {
return getCurrentActivity();
}
};
FutureTask<Activity> task = new FutureTask<>(callable);
getInstrumentation().runOnMainSync(task);
try {
return task.get(); // Blocks
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
Example on how to use:
@Override
public boolean checkCondition() {
Activity currentActivity = getCurrentActivitySafe();
(...)
}
getCurrentActivitySafe() has to be called instead getCurrentActivity() because checkCondition can run outside of the UI thread, causing an error.
My suggestion is to make getCurrentActivitySafe() an utility function, or to rename it to getCurrentActivity() and make it a method of Instruction.
Another alternative is to make checkCondition a method that receives the current activity as an argument, like this checkCondition(Activity currentActivity)
First, thanks for this tool! IdlingResource is a total mess.
Now to business, from the documentation I was not able to understand how to get the latest activity.
However, I found a different way, which follows:
Example on how to use:
getCurrentActivitySafe()has to be called insteadgetCurrentActivity()becausecheckConditioncan run outside of the UI thread, causing an error.My suggestion is to make
getCurrentActivitySafe()an utility function, or to rename it togetCurrentActivity()and make it a method ofInstruction.Another alternative is to make
checkConditiona method that receives the current activity as an argument, like thischeckCondition(Activity currentActivity)