Kevin D. Kevin D. - 1 year ago 67
Android Question

How to reference an Activity from within an Aspect

I currently have the current aspect

public class ActivityShowingAspect {

private static final String POINTCUT_METHOD =
"execution( * *(..))";

public void methodAnnotatedWithActivityShowing() {

public Object weaveJoinPoint(ProceedingJoinPoint joinPoint) throws Throwable {
Activity activity = // code to retrieve the calling activity, joinPoint.getTarget() or whatever
Object result = null;
if(!activity.isFinishing()) {
result = joinPoint.proceed();
} else {
result = // do something else
return result;


I'd like to know how to determine the calling
from within the Aspect.

Answer Source

Okay so it depends on where the method with your annotation is.

If the annotated method is declared within an Activity implementation, then you can indeed call joinpoint.getTarget()and cast the result.

Also you might want to update your pointcut to make sure that the method indeed is on an activity :

 execution( * *(..)) && within(

If that's not the case then you might need to add an advice before any activity's onResume() to remember what the current activity is.