0

I currently have the current aspect

@Aspect
public class ActivityShowingAspect {

    private static final String POINTCUT_METHOD =
            "execution(@nz.co.kevinsahandsomedevil.android.myaccount.aspect.ActivityMustBeShowing * *(..))";

    @Pointcut(POINTCUT_METHOD)
    public void methodAnnotatedWithActivityShowing() {
    }

    @Around("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 Activity from within the Aspect.

Kevin D.
  • 1,500
  • 1
  • 18
  • 41
  • Aren't you already doing that with the `!activity.isFinishing()` in your code ? Can you be more descriptive about what you consider not finishing ? – XGouchet Aug 17 '16 at 07:47
  • @XGouchet hi editted the question - I'm actually asking how to retrieve the Activity from within the aspect. Was confused after reading it again. – Kevin D. Aug 17 '16 at 22:14

1 Answers1

1

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(@nz.co.vodafone.android.myaccount.aspect.ActivityMustBeShowing * *(..)) && within(android.app.Activity+)

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.

XGouchet
  • 10,002
  • 10
  • 48
  • 83
  • Thanks - I'm not going to restrict it within an Activity since I'm planning on calling it within Fragments as well, but that makes sense. Just a quick follow up; what should be my `result` within `else`? (code above) – Kevin D. Aug 17 '16 at 22:31
  • 1
    Well it depends on the type redirected by your method, so you probably want either to return null (which cover any object), or add a parameter to your annotation to set the default value to return : @ActivityMustBeShowing(defaultValue = "foo") – XGouchet Aug 17 '16 at 22:34