Android – Handling Events

If you need to control or monitor the processes that take place on the purchase screen, you need to implement the AdaptyUiEventListener methods.

If you would like to leave the default behavior in some cases, you can extend AdaptyUiDefaultEventListener and override only those methods you want to change.

Below are the defaults from AdaptyUiDefaultEventListener.

User Generated Events

Closing the paywall

If the close button is visible and the user presses it, this method will be invoked:

public override fun onCloseButtonClick(view: AdaptyPaywallView) {
    (view.context as? Activity)?.onBackPressed()
}

🚧

This method is not invoked when user presses the system back button instead of the close icon on the screen.

Canceled purchase

If the user initiates the purchase process but manually interrupts it, this function will be called. Basically, this event occurs when the Adapty.makePurchase() function completes with a USER_CANCELED error:

public override fun onPurchaseCanceled(
    product: AdaptyPaywallProduct,
    view: AdaptyPaywallView,
) {}

Successful purchase

If Adapty.makePurchase() succeeds, this method will be invoked:

public override fun onPurchaseSuccess(
    profile: AdaptyProfile?,
    product: AdaptyPaywallProduct,
    view: AdaptyPaywallView,
) {
    (view.context as? Activity)?.onBackPressed()
}

We recommend to dismiss the screen in that case.

Failed purchase

If Adapty.makePurchase() fails, this method will be invoked:

public override fun onPurchaseFailure(
    error: AdaptyError,
    product: AdaptyPaywallProduct,
    view: AdaptyPaywallView,
) {}

Successful restore

If Adapty.restorePurchases() succeeds, this method will be invoked:

public override fun onRestoreSuccess(
    profile: AdaptyProfile,
    view: AdaptyPaywallView,
) {}

We recommend to dismiss the screen if the user has the required accessLevel.

Failed restore

If Adapty.restorePurchases() fails, this method will be invoked:

public override fun onRestoreFailure(
    error: AdaptyError,
    view: AdaptyPaywallView,
) {}

Data Fetching and Rendering

Products Loading Errors

If you didn't pass the products during initialization, AdaptyUI will retrieve the necessary objects from the server by itself. In this case, this operation may fail, and AdaptyUI will report the error by invoking this method:

public override fun onLoadingProductsFailure(
    error: AdaptyError,
    view: AdaptyPaywallView,
): Boolean = false

If you return true, AdaptyUI will repeat the request after 2 seconds.

Rendering Errors

If an error occurs during the interface rendering, it will be reported by calling this method:

public override fun onRenderingError(
    error: AdaptyError,
    view: AdaptyPaywallView,
) {}

In a normal situation such errors should not occur, so if you come across one, please let us know about it.

Overriding Behaviors

URL opening behavior

By default, AdaptyUI will show system sheet with installed apps that can handle the URL. If you want to change this behavior, please implement the following method:

public override fun onUrlClicked(url: String, view: AdaptyPaywallView) {
    (view.context as? Activity)?.startActivity(
        Intent.createChooser(Intent(Intent.ACTION_VIEW, Uri.parse(url)), "") // the default behavior
    )
}

Presenting messages to user

We often have a situation where we need to show a message to a user. It can be, for example, an error that occurred during the purchase process, or, on the contrary, a message about successfully restored purchases. By default we show messages with texts in English using the AlertDialog, but if you need to customize this behavior, implement the following method:

public override fun showAlert(
    event: AdaptyUI.Event,
    view: AdaptyPaywallView,
) {
    //creating and showing the default dialog
}

🚧

If you override this method without calling super.showAlert(), be sure to call doAfterAlert(event, view) (in case you extend AdaptyUiDefaultEventListener) or implement the logic equivalent to that from doAfterAlert() (in case you implement AdaptyUiEventListener directly) so that the AdaptyUI understands when you can continue.

In the default implementation it is called on dismissing the dialog.