Creating a CanvasLifecycleHandler

You can control your app lifecycle by providing an implementation of the Canvas.CanvasLifecycleHandler Apex interface that Salesforce can use.

The Apex Canvas.CanvasLifecycleHandler interface provides methods and callbacks for customizing app lifecycle behavior. Salesforce will use your implementation at runtime to let you run custom code. Use the following steps to create an implementation of the Canvas.CanvasLifecycleHandler interface.

  1. From Setup, enter Apex Classes in the Quick Find box, then select Apex Classes.
  2. Click New to create a Apex class.
  3. Create an Apex class that implements the Canvas.CanvasLifecycleHandler interface. You must implement the excludeContextTypes() and onRender() methods. Here’s a template example:
    public class MyCanvasLifecycleHandler 
    implements Canvas.CanvasLifecycleHandler {
    
        public Set<Canvas.ContextTypeEnum> excludeContextTypes(){
            Set<Canvas.ContextTypeEnum> excluded = new Set<Canvas.ContextTypeEnum>();
    
            // Code goes here to add items to excluded list
            // that should be excluded from Context data
    
            return excluded;
        }
    
        public void onRender(Canvas.RenderContext renderContext) {
    
            // Code goes here to customize behavior when the app is rendered
    
        }
    }
  4. After you’ve finished adding your code, save the Apex class.
  5. Optionally test your implementation by using the Canvas.Test class.
  6. To let Salesforce know which implementation to use for your app, associate your Apex class with your app.