-
Notifications
You must be signed in to change notification settings - Fork 23
Open
Description
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)
Metadata
Metadata
Assignees
Labels
No labels