Listen for mini-program lifecycle events
The super app can listen for the mini-program lifecycle events to perform specific operations when IAPMiniProgram SDK launches or terminates a mini program. To listen for these events, the super app must implement the GriverAppEvent
interface. This guide covers the necessary implementation procedures and interface references.
Note: Implementations of lifecycle event listeners do not block mini programs from opening or closing. Therefore, these listeners are not suitable for scenarios where the super app requires the user to complete a specific action before closing a mini program.
Procedures
To listen for mini-program lifecycle events, complete the following steps:
Step 1: Implement GriverAppEvent
In your app project, create a new file and define a class that implements the GriverAppEvent
interface. Within the class, override the following methods:
onAppStart
andonAppExit
:GriverAppEvent
's declared methods for handling the launch and exit events of mini programs.onInitialized
andonFinalized
:GriverAppEvent
's inherited methods from theExtension
interface.
Refer to the following code samples for implementation:
Kotlin
class GriverAppEventImpl: GriverAppEvent {
override fun onInitialized() {}
override fun onFinalized() {}
override fun onAppStart(app: App?) {}
override fun onAppExit(app: App?) {}
}
Java
class GriverAppEventImpl implements GriverAppEvent {
@Override
public void onAppStart(App app) {}
@Override
public void onAppExit(App app) {}
@Override
public void onInitialized() {}
@Override
public void onFinalized() {}
}
Step 2: Register the implementation
After the SDK initialization logic, call the registerEventHandler API to register the implemented GriverAppEvent
interface with the SDK. Refer to the following code samples for registration:
Kotlin
class MyApplication: Application() {
override fun onCreate() {
...
IAPConnect.init(this, initConfig, object : InitCallback {
override fun onSuccess() {
Griver.registerEventHandler(
GriverEventManifest(
GriverAppEventImpl::class.java.name,
listOf(GriverAppEvent::class.java.name),
App::class.java
)
) { errorCode, errorMessage ->
Log.e(TAG, "$errorCode: $errorMessage")
}
}
...
})
}
}
Java
public class MyApplication extends Application {
@Override
protected onCreate() {
...
IAPConnect.init(this, initConfig, new InitCallback() {
@Override
public void onSuccess() {
Griver.registerEventHandler(
new GriverEventManifest(
GriverAppEventImpl.class.getName(),
Arrays.asList(GriverAppEvent.class.getName()),
App.class
),
(errorCode, errorMessage) -> {
Log.e(TAG, "$errorCode: $errorMessage");
}
);
}
...
});
}
}
Interface
GriverAppEvent
The GriverAppEvent
interface is used to listen for mini-program lifecycle events. The following code shows the interface definition:
public interface GriverAppEvent extends GriverEvent {
void onAppStart(App app);
void onAppExit(App app);
}
GriverAppEvent
provides the following two methods:
Parameter
The onAppStart
and onAppExit
methods take the following parameter, which is passed by the SDK:
Name | Type | Length | Description | Required |
app | App | N/A | The mini-program instance. | M |