Start a Mini Program

更新时间:
复制 MD 格式

This topic describes the APIs for starting a Mini Program and provides examples.

API description

Mriver.startApp(String appid)

Use this API to start a Mini Program.

Code example

Mriver.startApp("2021022320210223");

Parameters

Name

Type

Description

Required

appId

String

The AppID of the target Mini Program.

Yes

Mriver.startApp(Activity activity,String appid)

Use this API to start a Mini Program. You must pass the `Activity` of the page from which the Mini Program is started.

Code example

Mriver.startApp(activity, "2021022320210223");

Parameters

Name

Type

Description

Required

appId

String

The AppID of the target Mini Program.

Yes

activity

Activity

The Activity where the Mini Program is started.

No (Recommended)

Mriver.startApp(Activity activity,String appid,Bundle bundle)

Use this API to start a Mini Program. You must pass the `Activity` of the page from which the Mini Program is started.

Code example

Bundle bundle = new Bundle();
bundle.putString("page", "pages/index/index"); // Set the path
bundle.putString("query", "name=123&pwd=456"); // Set the parameters
Mriver.startApp(activity, "2021022320210223", bundle);

Parameters

Name

Type

Description

Required

appId

String

The AppID of the target Mini Program.

Yes

activity

Activity

The Activity where the Mini Program is started.

No (Recommended)

bundle

Bundle

The parameters for starting the Mini Program.

No

Start a Mini Program

Start a Mini Program and pass custom parameters

Mriver.startApp(Activity activity,String appid,Bundle bundle)

In some scenarios, you may need to pass parameters to the default entry page of a miniapp, pages/index/index. This topic uses an example to show how to pass the name and pwd parameters.

  1. On the client, add the parameters to be passed at startup.

    Bundle bundle = new Bundle();
    bundle.putString("query", "name=123&pwd=456"); // Set the parameters
    Mriver.startApp(activity, "2021022320210223", bundle);
  2. The query field is used to pass startup parameters. To retrieve the parameters, you must parse the query field. The startApp parameters are described as follows:

    • appId: The ID of the Mini Program. You can find this ID in the mPaaS console.

    • bundle: A Bundle object for passing request parameters. Set the key to `query` and the value to the key-value pairs. Separate multiple parameters with an ampersand (&).

  3. Retrieve the parameters from the options parameter of the onLaunch/onShow(options) method in the Mini Program.

    When app.js loads, parameters passed from the client to the miniapp are retrieved and stored in the global variable globalData. You can directly retrieve or update these values from globalData as needed. Parameters in the request header, such as token and user_id, are passed from Native and also stored in globalData.

Start a Mini Program and navigate to a specific page

Mriver.startApp(Activity activity,String appid,Bundle bundle)

You can navigate to a specific page of the Mini Program. If a page is not specified, the path defaults to the configured homepage.

  1. On the client, add the parameter that specifies the page to navigate to at startup.

    Bundle bundle = new Bundle();
    bundle.putString("page", "pages/index/index"); // Set the path
    Mriver.startApp(activity, "2021022320210223", bundle);
  2. When you launch an application using a URL, pass parameters in the query field. You can retrieve these parameters by parsing the query field. The parameters for startApp are as follows:

    • activity: The Activity page from which the Mini Program is started.

    • appId: The ID of the Mini Program. You can find this ID in the mPaaS console.

    • bundle: A Bundle object. Pass the request parameters to the Bundle object, where key="page" and value="path to the Mini Program page to open".

FAQ

Q: What is the purpose of passing an Activity when starting a Mini Program?

A: If you do not pass an Activity object, the container uses ApplicationLifecycle to record the Activity with a weak reference. If the system is out of memory, the Java Virtual Machine (JVM) may garbage collect the weakly referenced Activity object. This sets the object to null and causes the activity.startActivity call inside the container to fail.