AppEventListener (listen for the 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 the mini program. The IAPMiniProgram SDK calls the AppEventListener interface to monitor the opening and closing events of mini program.

Procedures

When initializing the SDK, register a listener to count the number of active mini-program instances. The following code installs a "counter listener" (appCount) into the SDK, which increments by 1 when a mini program starts, decrements by 1 when it exits, allowing you to track in real-time how many mini programs are currently active.

To listen for mini program lifecycle events, complete the following steps:

  1. Declare a "listener" class that implements the framework-provided AppEventListener interface.
  2. In the AppEventListener interface, implement the onAppStart and onAppExit callback methods. See the following Methods section.

Code sample

copy
class TestAppEventListener implements AppEventListener {
  int appCount = 0;
  
  @override
  Future<void> onAppStart(Map<String,dynamic> params){
    appCount++;
    return Future.value();
  }

  @override
  Future<void> onAppExit(Map<String,dynamic> params){
    appCount--;
    return Future.value();
  }
}

void initSDK() async {
  IAPMiniProgram.instance.init(
      appEventListener: TestAppEventListener()
  );
}

Methods

The onAppStart and onAppExit methods take the following parameters, which are passed by the SDK:

onAppStart method

IAPMiniprogram SDK calls the onAppStart method to launch/start the mini program.

Method signature

copy
Future<void> onAppStart(Map<String,dynamic> params);

Parameters

Field

Data type

Required

Description

appId

String

Yes

The unique ID that is assigned by Mini Program Platform to identify a mini program. You can get the ID from the Mini Program Platform console or by fetching mini program information with the fetchApps API.

appVersion

String

No

The version that is assigned by Mini Program Platform.

It follows the major.minor.patch pattern. For example, "1.0.1".

appType

String

No

The mini program type.

startParams

Map

No

The startup parameters of the mini program.

onAppExit method

IAPMiniprogram SDK calls the onAppExit method to exit/terminate the mini program.

Method signature

copy
Future<void> onAppExit(Map<String,dynamic> params);

Parameters

See the previous Parameters table.