---
schema_version: "1"
id: 382dff2c1fbee94839e20047433a8e692f0f093fbc9a200666a8b1d454e8331c
title: Creating a CanvasLifecycleHandler
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/platform/canvas-framework/guides/platform-connect/using/lifecycle/canvas-creating-a-new-canvaslifecyclehandler.md
canonical_url: https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-creating-a-new-canvaslifecyclehandler.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 7cf89da751faaf459ef888c92fefa09bb1879aebf19e78963f5af9cdd1eecce3
products:
  - Canvas Developer Guide
  - Platform Developer Content
---

# 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:

    <!-- otherprops: set_clipboard -->

    ```apex
    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.

**See Also**

- [Testing Your CanvasLifecycleHandler Implementation](https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-testing-your-canvaslifecyclehandler.md)
- [Associating Your CanvasLifecycleHandler with Your App](https://developer.salesforce.com/docs/platform/canvas-framework/guide/canvas-associating-handler-with-app.md)
