PortalMethod
The @PortalMethod annotation is used to easily create functions to use with the PortalsPlugin. A function with a @PortalMethod can be triggered by using the Portals.sendMessage() function from the PortalsPlugin class with the message
parameter on the web code matching the function name in the native code.
The below example shows how to setup a function nativeFunction
on an Android Fragment and call it from the web code.
Android
- Kotlin
- Java
class MyPortalFragment : PortalFragment() {
// ...
public fun onCreate(savedInstanceState: Bundle?): Unit {
super.onCreate(savedInstanceState)
// ...
this.linkMessageReceivers(this)
}
@PortalMethod
public fun nativeFunction(payload: String): Unit {
// run native code here
}
}
class MyPortalFragment extends PortalFragment {
// ...
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
this.linkMessageReceivers(this);
}
@PortalMethod
public void nativeFunction(String payload) {
// run native code here
}
}
Web
import { Portals } from "@native-portal/portals";
Portals.sendMessage({ message: "nativeFunction", payload: result });