MicroStrategy ONE
Customize the App Lifecycle for Android
Library Mobile SDK for Android supports callbacks for application state changes such as application launching, transitioning to the foreground or background, or application termination. You can customize the application lifecycle using the AppDelegate interface.
The following table lists the methods in com.microstrategy.android.AppDelegate:
Return Type |
Method and Description |
---|---|
void |
onApplicationWillFinishLaunchingWithIntent(Intent intent) @param Intent intent intent is the reason that launches the application, such as launching a URL. Called when users tap an icon or URL to launch the app. |
void |
onApplicationDidEnterForeground() Called when app transitions to foreground. Foreground is a state where users can view and interact with the app. |
void |
onApplicationDidEnterBackground() Called when the app transitions to background. Background means the app is running but cannot be viewed. This is typically triggered when the user taps the device's Home button. |
void |
onApplicationWillTerminate() Called when the app is terminated by the user tapping the Back button. |
Implement your own application delegate logic
- Set Up the Library Mobile Project for Android. You are using this project as the basis for your customizations.
- Next, let's implement the AppDelegate interface. Open the project in Android Studio.
- Switch your Tool window to the Project perspective.
- Navigate to src > main.
-
Right-click the main folder and create a new directory named java, to hold all of your Java source files.
-
Under the java directory, create a new package named com.example.
-
Create a new java class named CustomizedAppDelegate under the package that implements the AppDelegate interface.
-
Implement the methods defined in the AppDelegate interface.
Expand to see a code sample showing where to add your own logic.Copypublic class CustomizedAppDelegate implements AppDelegate {
@Override
public void onApplicationWillFinishLaunchingWithIntent(Intent intent) {
if (null != intent) {
Uri uri = intent.getData();
if (uri == null) {
return;
} else{
// Your own logic using uri
}
}
}
@Override
public void onApplicationDidEnterForeground() {
//Your own logic when application enters foreground
}
@Override
public void onApplicationDidEnterBackground() {
//Your own logic when application enters background
}
@Override
public void onApplicationWillTerminate() {
//Your own logic when application will Terminate
}
} - Navigate to src > main > AndroidManifest.xml.
-
Configure your customizedAppDelegate as a meta-data tag under the application tag. The key is com.microstrategy.android.applicationDelegate.
Copy<application... >
...
<meta-data android:name="com.microstrategy.android.applicationDelegate" android:value="com.example.CustomizedAppDelegate"/>
...
</application> - Clean and rebuild the project.
Important points to consider
If you implement the AppDelegate interface and write your own logic in onApplicationWillFinishLaunchingWithIntent, do not add anything that is too time-consuming, since it can block the main thread and startup activity creation. If you need to implement something time-consuming, it's better to create your own activity and add your logic as shown in Customize Startup Activity for Android
If you customize your own startup activity and you want to implement the AppDelegate callbacks, you need to modify the stackBottomActivity configuration in AndroidManifest.xml. Change the activity of the com.microstrategy.android.stackBottomActivity key to one at the bottom of the activity stack when the app is running, as shown below.
<application... >
...
<meta-data android:name="com.microstrategy.android.applicationDelegate" android:value="com.example.CustomizedAppDelegate"/>
<meta-data android:name="com.microstrategy.android.stackBottomActivity" android:value="com.microstrategy.android.CustomizedStartupActivity"/>
...
</application>