Execute code before activity

For a community extension I am building, I would like to have some code executed before certain activities, which are define during runtime. For example it could look like this:

if (nextActivity.condition)
execute nextActivity;
execute nextActivity.next;

Where would it be best to place said code?

Thanks for your help