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
Actions
If user has performed some action, this method will be invoked:
override fun onActionPerformed(action: AdaptyUI.Action, view: AdaptyPaywallView) {
when (action) {
AdaptyUI.Action.Close -> (view.context as? Activity)?.onBackPressed()
is AdaptyUI.Action.OpenUrl -> //launching intent to open url
is AdaptyUI.Action.Custom -> //no default action
}
}
At the moment there are three types of actions supported: Close
, OpenUrl(url)
and Custom(id)
. For example, if user presses the close button, the action close
will occur and you are supposed to dismiss the paywall.
This method is not invoked when user presses the system back button instead of the close icon on the screen.
Login Action
If you have configured Login Action in the dashboard, you should implement reaction for custom action with id
"login"
Product selection
If product was selected for purchase (by user or by system), this method will be invoked.
public override fun onProductSelected(
product: AdaptyPaywallProduct,
view: AdaptyPaywallView,
) {}
Started purchase
If user initiates the purchase process, this method will be invoked.
public override fun onPurchaseStarted(
product: AdaptyPaywallProduct,
view: AdaptyPaywallView,
) {}
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.
Updated 2 months ago