Respond to flow actions - Unity
If you are building flows or paywalls using the Adapty Flow Builder or Paywall Builder, it’s crucial to set up buttons properly:
- Add a button in the builder and assign it either a pre-existing action or create a custom action ID.
- Write code in your app to handle each action you’ve assigned.
This guide shows how to handle custom and pre-existing actions in your code.
Only purchases and restorations are handled automatically. All other button actions, such as closing flows or opening links, require proper response implementation in the app code.
Set up the flows events listener
To handle flow actions, implement the IAdaptyFlowsEventsListener interface and register it with Adapty.SetFlowsEventsListener(). This should be done early in your app’s lifecycle, typically in your main scene or app initialization.
using AdaptySDK;
public class FlowsListener : MonoBehaviour, IAdaptyFlowsEventsListener
{
void Start()
{
Adapty.SetFlowsEventsListener(this);
}
// implement all IAdaptyFlowsEventsListener methods here
}IAdaptyFlowsEventsListener is a C# interface, so implement all of its methods — see Handle flow & paywall events for the full list. The examples below show only the FlowViewDidPerformAction method.
All button actions arrive in the FlowViewDidPerformAction(view, action) callback as an AdaptyUIUserAction object with a Type of Close, SystemBack, OpenUrl, or Custom, plus an optional Value (the URL or the custom action ID).
Close flows and paywalls
To add a button that will close your flow or paywall:
- In the builder, add a button and assign it the Close action.
- In your app code, implement a handler for the
Closeaction that dismisses the flow.
A dismissed view is destroyed and can’t be presented again. To display the flow one more time, call CreateFlowView again.
public void FlowViewDidPerformAction(
AdaptyUIFlowView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Close:
view.Dismiss(null);
break;
default:
// handle other events
break;
}
}Handle the Android system back button
Pressing the Android system back button (or using the back gesture) emits an action of the SystemBack type. It does not dismiss the flow on its own — the flow stays open, and the user leaves it through a path you define, such as a Close button or an on_device_back action in the builder. If you want the system back button to dismiss the flow, handle the action yourself:
public void FlowViewDidPerformAction(
AdaptyUIFlowView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Close:
case AdaptyUIUserActionType.SystemBack:
view.Dismiss(null);
break;
default:
// handle other events
break;
}
}Open URLs from flows and paywalls
If you want to add a group of links (e.g., terms of use and purchase restoration), add a Link element in the builder and handle it the same way as buttons with the Open URL action.
To add a button that opens a link from your flow or paywall (e.g., Terms of use or Privacy policy):
- In the builder, add a button, assign it the Open URL action, and enter the URL you want to open.
- In your app code, implement a handler for the
OpenUrlaction that opens the received URL.
Use the AdaptyUI.OpenUrl method to open the URL natively, honoring the external or in-app browser option (action.OpenIn) configured in the builder:
public void FlowViewDidPerformAction(
AdaptyUIFlowView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.OpenUrl:
var urlString = action.Value;
if (!string.IsNullOrWhiteSpace(urlString)) {
AdaptyUI.OpenUrl(
urlString,
action.OpenIn ?? AdaptyWebPresentation.ExternalBrowser,
(error) => {
// handle the error
}
);
}
break;
default:
// handle other events
break;
}
}Log into the app
To add a button that logs users into your app:
- In the builder, add a button and assign it the Custom action with ID
login. - In your app code, implement a handler for the
logincustom action that identifies your user.
public void FlowViewDidPerformAction(
AdaptyUIFlowView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Custom:
if (action.Value == "login") {
// Navigate to login scene
SceneManager.LoadScene("LoginScene");
}
break;
default:
// handle other events
break;
}
}Handle custom actions
To add a button that handles any other actions:
- In the builder, add a button, assign it the Custom action, and assign it an ID.
- In your app code, implement a handler for the action ID you’ve created.
For example, if you have another set of subscription offers or one-time purchases, you can add a button that will display another flow or paywall:
public void FlowViewDidPerformAction(
AdaptyUIFlowView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Custom:
if (action.Value == "openNewFlow") {
// Display another flow or paywall
ShowAlternativeFlow();
}
break;
default:
// handle other events
break;
}
}
private void ShowAlternativeFlow() {
// Implement your logic to show an alternative flow
}If you are building paywalls using the Adapty paywall builder, it’s crucial to set up buttons properly:
- Add a button in the paywall builder and assign it either a pre-existing action or create a custom action ID.
- Write code in your app to handle each action you’ve assigned.
This guide shows how to handle custom and pre-existing actions in your code.
Only purchases and restorations are handled automatically. All the other button actions, such as closing paywalls or opening links, require implementing proper responses in the app code.
Close paywalls
To add a button that will close your paywall:
- In the paywall builder, add a button and assign it the Close action.
- In your app code, implement a handler for the
closeaction that dismisses the paywall.
public void PaywallViewDidPerformAction(
AdaptyUIPaywallView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Close:
view.Dismiss(null);
break;
default:
// handle other events
break;
}
}Open URLs from paywalls
If you want to add a group of links (e.g., terms of use and purchase restoration), add a Link element in the paywall builder and handle it the same way as buttons with the Open URL action.
To add a button that opens a link from your paywall (e.g., Terms of use or Privacy policy):
- In the paywall builder, add a button, assign it the Open URL action, and enter the URL you want to open.
- In your app code, implement a handler for the
openUrlaction that opens the received URL in a browser.
public void PaywallViewDidPerformAction(
AdaptyUIPaywallView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.OpenUrl:
var urlString = action.Value;
if(!string.IsNullOrWhiteSpace(urlString)) {
Application.OpenURL(urlString);
}
break;
default:
// handle other events
break;
}
}Log into the app
To add a button that logs users into your app:
- In the paywall builder, add a button and assign it the Custom action with ID
login. - In your app code, implement a handler for the
logincustom action that identifies your user.
public void PaywallViewDidPerformAction(
AdaptyUIPaywallView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Custom:
if (action.Value == "login") {
// Navigate to login scene
SceneManager.LoadScene("LoginScene");
}
break;
default:
// handle other events
break;
}
}Handle custom actions
To add a button that handles any other actions:
- In the paywall builder, add a button, assign it the Custom action, and assign it an ID.
- In your app code, implement a handler for the action ID you’ve created.
For example, if you have another set of subscription offers or one-time purchases, you can add a button that will display another paywall:
public void PaywallViewDidPerformAction(
AdaptyUIPaywallView view,
AdaptyUIUserAction action
) {
switch (action.Type) {
case AdaptyUIUserActionType.Custom:
if (action.Value == "openNewPaywall") {
// Display another paywall
ShowAlternativePaywall();
}
break;
default:
// handle other events
break;
}
}
private void ShowAlternativePaywall() {
// Implement your logic to show alternative paywall
}