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
MainActivity
in the template app) to point to an activity that does not extend any of the following Mobile SDK classes:SalesforceActivity
SalesforceListActivity
SalesforceExpandableListActivity
This rule also applies to any other activities that run before you authenticate with Salesforce.
- Do not call
ClientManager.
peekRestClient()
orClientManager.
getRestClient()
from your launcher activity or from any other pre-authentication activities. - Do not change the
initNative()
method in theTemplateApp
class. This method must point to the first activity class that launches after Salesforce authentication, which isMainActivity
in the template app. - When you’re ready to authenticate with Salesforce, launch the
MainActivity
class.
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"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:id="@+id/root">
<Button android:id="@+id/login_button"
android:layout_width="80dp"
android:layout_height="60dp"
android:text="@string/login"
android:textColor="@android:color/black"
android:textStyle="bold"
android:gravity="center"
android:layout_gravity="center"
android:textSize="18sp"
android:onClick="onLoginClicked" />
</LinearLayout>
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 peekRestClient()
or 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 android.intent.category.DEFAULT
.
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 MainActivity
.