When you use forcedroid to create Salesforce Mobile SDK for Android apps, forcedroid bases your project on a template app. The template app gives you lots of free standard functionality. For example, you don’t have to implement authentication—login and passcode handling are built into your launcher activity. This design works well for most apps, and the free code is a big time-saver. However, after you’ve created your forcedroid app you might find reasons for deferring Salesforce authentication until some point after launch.
As the example below shows, you can defer authentication easily and still keep the template app’s built-in functionality. Just be sure to follow these guidelines:
- Change the launcher activity setting (which references
MainActivityin the template app) to point to an activity that does not extend any of the following Mobile SDK classes:
This rule also applies to any other activities that run before you authenticate with Salesforce.
- Do not call
getRestClient()from your launcher activity or from any other pre-authentication activities.
- Do not change the
initNative()method in the
TemplateAppclass. This method must point to the first activity class that launches after Salesforce authentication, which is
MainActivityin the template app.
- When you’re ready to authenticate with Salesforce, launch the
The following example shows how to insert a non-Salesforce launcher activity ahead of Salesforce authentication. You can of course embellish this example with additional pre-authentication activities, following the guidelines and caveats discussed above.
1. Create an XML layout for the pre-authentication landing page of your application. The following layout file,
launcher.xml, is a bare-bones example that contains a single login button.
Here’s the launcher.xml listing:
<?xml version="1.0" encoding="utf-8"?>
Note This layout uses a string resource,
@string/login, that is defined in the res/strings.xml file as follows:
<string name="login">Log in</string>
2. Create a landing screen activity to launch the app. For example, here’s one named
LauncherActivity. This screen simply inflates the XML layout defined in launcher.xml. This class must not extend any of the Salesforce activities or call
getRestClient(), since these calls trigger the authentication flow.
3. Modify AndroidManifest.xml to specify
LauncherActivity as the activity to be launched when the app first
starts. Also, change the category of the
MainActivity class to
When you start the application. the
LauncherActivity screen appears. Click the login button to initiate the Salesforce authentication flow. After authentication completes, the app launches