[Samsung DeX] Lifecycle on switching between Mobile and Samsung DeX mode

Samsung Developer Program

Samsung DeX is a new way to launch your android application in a desktop like environment. Upon opening Samsung DeX Mode, every app that stayed open in Phone mode, undergo certain changes before launching in Samsung DeX.

In this blog I will be telling you what exactly happens when we switch mode from phone mode to Samsung DeX mode or vice versa.

Switching between phone and Samsung DeX mode.

By default, applications are automatically killed when they switch between mobile mode to Samsung DeX mode or vice versa. You can assume that behaviour is like pressing the home button. When user moves from phone mode to Samsung DeX mode, then all the opened application will move to taskbar. Once you click application from taskbar, it will launch again.

Before kill process, the status of Application is changed to "Stopped".

After complete switching, shortcuts on taskbar has only tasks (application) information and when we select shortcut, application would be newly created.

Now the question comes how to prevent application from killing during mode switch i.e. phone to DeX or vice versa?

To prevent killing process, application could use "keepalive" declaration.

Declare the keepalive attribute to true in your manifest file like below:

<meta-data android:name="com.samsung.android.keepalive.density" android:value="true" />

In case of application declaring "keepalive", application would be not killed on switching after application’s status changing to "Stopped".

But, if the application does not add the android:configChanges flag to the activity's Manifest, application would be destroyed and recreated like the case of resizing the window.

During mode switch, screen size i.e. screen width, screen height, and screen layout, smallest screen size, screen orientation, UI mode, density are changed. To prevent applications from recreating after switching the mode, android:configChanges flag to the activity's Manifest is required. We need to add the following configuration changes in manifest file for your activity.

<activity android:configChanges="orientation | screenSize | screenLayout | smallestScreenSize | uiMode | density">

So, if we declare both keepalive and android:configChanges flag to the activity's Manifest then there will be no killing of the application. In this case when we re-launch app via shortcut in taskbar, the functions are invoked according to the following sequence.

onConfigurationChanged(), onRestart(), onStart(), onResume() callbacks are sequentially called.

For more information about how the android life cycle works in Samsung DeX mode, you can refer to: Android life cycle blog here.