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.
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);The
queryfield is used to pass startup parameters. To retrieve the parameters, you must parse thequeryfield. ThestartAppparameters are described as follows:appId: The ID of the Mini Program. You can find this ID in the mPaaS console.
bundle: A
Bundleobject for passing request parameters. Set thekeyto `query` and thevalueto the key-value pairs. Separate multiple parameters with an ampersand (&).
Retrieve the parameters from the
optionsparameter of theonLaunch/onShow(options)method in the Mini Program.When
app.jsloads, parameters passed from the client to the miniapp are retrieved and stored in the global variableglobalData. You can directly retrieve or update these values fromglobalDataas needed. Parameters in the request header, such astokenanduser_id, are passed fromNativeand also stored inglobalData.
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.
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);When you launch an application using a URL, pass parameters in the
queryfield. You can retrieve these parameters by parsing thequeryfield. The parameters forstartAppare as follows:activity: The
Activitypage 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
Bundleobject. Pass the request parameters to theBundleobject, wherekey="page"andvalue="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.