Topics covered:
Requirements:
A custom action is an action with a specific behavior for your use case. Beagle has a number of default actions, however, there may be situations that require new functionalities, such as starting the camera interface on a cell phone.
For more information about Default Beagle actions, check out action-types section..
To create a custom action you need to:
1. Create a class annotated with @RegisterAction
, and implement the Action
interface, in your application Backed and Frontend environments;
2. Set the action name by annotation parameter to avoid possible problems with Proguard. Make sure the action has the same name in the Backed and Frontend;
** 3.** Implement the **execute
** method (only in the **Frontend**).
The value
attribute is an example of a parameter that can be declared in the Action class constructor, you can set as many as you need.
The following example shows a custom action that will execute a Toast receiving a text as the value
parameter:
@RegisterAction("customActionAndroid")
data class CustomActionAndroid(
val value: String
) : Action {
override fun execute(rootView: RootView) {
Toast.makeText(
rootView.getContext(),
value,
Toast.LENGTH_SHORT
).show()
}
}
@RegisterAction("customActionAndroid")
data class CustomActionAndroid(
val value: String
) : Action
value
that will store the message to be used in TOAST.See below, a JSON that represents this action being called from the click of a button:
{
"_beagleComponent_": "beagle:button",
"text": "Beagle Button",
"onPress": [
{
"_beagleAction_": "custom:myCustomAction",
"value": "ParameterForAction."
}
]
}
As with default actions, to use a custom action, just declare it in a component that triggers events, such as a Button
’s on press. See below:
Button(
text = text,
styleId = styleId,
onPress = listOf(
CustomActionAndroid("I am a custom action")
)
)
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.