MainApplication Class

Every native Android app requires an instance of android.app.Application. The MainApplication class accomplishes these basic tasks:

  • Overrides the Android Application.onCreate() method.
  • In its onCreate() override:
    • Calls the superclass onCreate() method.
    • Initializes Salesforce Mobile SDK by calling initNative() on the SDK manager object (MobileSyncSDKManager).
    • Provides optional commented code that you can reinstate to use your app as a Salesforce identity provider.
    • Provides optional commented code that you can reinstate to support push notifications.

Here’s the entire class:

Kotlin
1package com.bestapps.android
2
3import android.app.Application
4import com.salesforce.androidsdk.mobilesync.app.MobileSyncSDKManager
5
6/**
7 * Application class for our application.
8 */
9class MainApplication : Application() {
10
11    companion object {
12        private const val FEATURE_APP_USES_KOTLIN = "KT"
13    }
14
15    override fun onCreate() {
16        super.onCreate()
17        MobileSyncSDKManager.initNative(applicationContext, MainActivity::class.java)
18        MobileSyncSDKManager.getInstance().registerUsedAppFeature(FEATURE_APP_USES_KOTLIN)
19        /*
20         * Uncomment the following line to enable IDP login flow. This will allow the user to
21         * either authenticate using the current app or use a designated IDP app for login.
22         * Replace 'idpAppURIScheme' with the URI scheme of the IDP app meant to be used.
23         */
24        // MobileSyncSDKManager.getInstance().idpAppURIScheme = idpAppURIScheme
25
26        /*
27		 * Un-comment the line below to enable push notifications in this app.
28		 * Replace 'pnInterface' with your implementation of 'PushNotificationInterface'.
29		 * Add your Google package ID in 'bootonfig.xml', as the value
30		 * for the key 'androidPushNotificationClientId'.
31		 */
32        // MobileSyncSDKManager.getInstance().pushNotificationReceiver = pnInterface
33    }
34}
Java
1package com.salesforce.androidnativetemplate;
2import android.app.Application;
3import com.salesforce.androidsdk.mobilesync.app.MobileSyncSDKManager;
4/**
5 * Application class for our application.
6 */
7public class MainApplication extends Application {
8    @Override
9    public void onCreate() {
10        super.onCreate();
11        MobileSyncSDKManager.initNative(getApplicationContext(), MainActivity.class);
12
13        /*
14         * Uncomment the following line to enable IDP login flow. This will allow the user to
15         * either authenticate using the current app or use a designated IDP app for login.
16         * Replace 'idpAppURIScheme' with the URI scheme of the IDP app meant to be used.
17         */
18        // MobileSyncSDKManager.getInstance().setIDPAppURIScheme(idpAppURIScheme);
19
20        /*
21         * Un-comment the line below to enable push notifications in this app.
22         * Replace 'pnInterface' with your implementation of 'PushNotificationInterface'.
23         * Add your Google package ID in 'bootonfig.xml', as the value
24         * for the key 'androidPushNotificationClientId'.
25         */
26        // MobileSyncSDKManager.getInstance().setPushNotificationReceiver(pnInterface);
27    }
28}

Most native Android apps can use similar code. For this small amount of work, your app gets free implementations of passcode and login/logout mechanisms, plus a few other benefits. See SalesforceActivity, SalesforceListActivity, and SalesforceExpandableListActivity Classes.