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;
2
3import android.app.Application;
4import com.salesforce.androidsdk.mobilesync.app.MobileSyncSDKManager;
5/**
6* Application class for our application.
7*/
8public class MainApplication extends Application {
9    @Override
10    public void onCreate() {
11        super.onCreate();
12        MobileSyncSDKManager.initNative(getApplicationContext(), MainActivity.class);
13
14        /*
15        * Uncomment the following line to enable IDP login flow. This will allow the user to
16        * either authenticate using the current app or use a designated IDP app for login.
17        * Replace 'idpAppURIScheme' with the URI scheme of the IDP app meant to be used.
18        */
19        // MobileSyncSDKManager.getInstance().setIDPAppURIScheme(idpAppURIScheme);
20
21        /*
22        * Un-comment the line below to enable push notifications in this app.
23        * Replace 'pnInterface' with your implementation of 'PushNotificationInterface'.
24        * Add your Google package ID in 'bootonfig.xml', as the value
25        * for the key 'androidPushNotificationClientId'.
26        */
27        // MobileSyncSDKManager.getInstance().setPushNotificationReceiver(pnInterface);
28    }
29}

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.

We've Moved

Welcome to the new home of the Mobile SDK Developer Guide! For now, the Japanese guide can be found in PDF form.