71. Foldable Devices and Multi-Window Support

Foldable devices are coming whether we are ready for them or not (or, in the case of the first Samsung Galaxy Fold, perhaps even before the devices themselves are ready). In preparation for this new category of device, it is important to be aware of some additional steps that should be taken to ensure that your app performs correctly when running on a foldable device.

Fortunately, much of the behavior for supporting foldable devices already exists on Android in the form of Multi-Window support.

71.1 Foldables and Multi-Window Support

When an app is running on a foldable device there is the potential that it will end up sharing the screen with other apps and encountering significant configuration changes (such as the size of the screen changing as the user folds or unfolds the display). If your app is already designed to handle device orientation changes, it will most likely also be able to handle changes caused by screen folding, though thorough testing is recommended.

Multi-window support was originally introduced with Android 7. Unlike previous versions of Android, multi-window support in Android 7 allowed more than one activity to be displayed on the device screen at one time.

Multi-window support in Android provides three different forms of window support. Split-screen mode, available on most phone, foldable and tablet devices, provides a split screen environment where two activities appear either side by side or one above the other. A moveable divider is provided which, when dragged by the user, adjusts the percentage of the screen assigned to each of the adjacent activities:

Figure 71-1

Freeform mode provides a windowing environment on devices with larger screens and is currently enabled at the discretion of the device manufacturer. Freeform differs from split-screen mode in that it allows each activity to appear in a separate, resizable window and is not limited to two activities being displayed concurrently. Figure 71-2, for example, shows a device in freeform mode with the Calculator and a second app displayed in separate windows:

Figure 71-2

Picture-in-picture support, as the name suggests, allows video playback to continue in a smaller window while the user performs other tasks, a topic that will be covered beginning with the chapter entitled “Android Picture-in-Picture Mode”.

71.2 Using a Foldable Emulator

Although at time of writing there are no foldable devices on the market with which to perform app testing, foldable emulators are included with the Android SDK. To create a foldable emulator, select the Android Studio Tools -> AVD Manager menu option, click on the Create Virtual Device button and, from the hardware selection screen, choose one of the Foldable options as highlighted in Figure 71-3 below:

Figure 71-3

After making a foldable selection, continue through the creation process, selecting Android 10 API 29 or newer as the system image.

Once the emulator is up and running, an additional button will appear in the toolbar allowing the emulator to be switched between folded and unfolded configurations:

Figure 71-4

71.3 Entering Multi-Window Mode

Split-screen mode can be entered by displaying the Overview screen, pressing and holding the app icon in the toolbar of a listed app and selecting the Split screen menu option as indicated in Figure 71-5:

Figure 71-5

Once in split-screen mode, the Overview button will change to display two rectangles (marked A in Figure 71-6), the current activity will fill part of the screen (B) and the Overview screen will appear in the adjacent part of the screen allowing the second activity to be selected for display (C):

Figure 71-6

Once the second app has been selected, the screen will be split evenly as illustrated previously in Figure 71-1 above.

To exit split-screen mode, simply drag the divider separating the two activities to a far edge so that only one activity fills the screen, or press and hold the Overview button until it reverts to a single square.

71.4 Enabling and using Freeform Support

Although not officially supported on all devices, it is possible to enable freeform multi-window mode on large screen devices and emulators. To enable this mode, run the following adb command while the emulator is running, or the device is connected:

adb shell settings put global enable_freeform_support 1

After making this change, it may be necessary to reboot the device before the setting takes effect.

Once enabled, an additional option will appear within the Overview screen when performing a long press on the app icon as shown in Figure 71-7:

Figure 71-7

71.5 Checking for Freeform Support

As outlined earlier in the chapter, Google is leaving the choice of whether to enable freeform multi-window mode to the individual Android device manufacturers. Since it only makes sense to use freeform on larger devices, there is no guarantee that freeform will be available on every device on which an app is likely to run. Fortunately all of the freeform specific methods and attributes are ignored by the system if freeform mode is not available on a device, so using these will not cause the app to crash on a non-freeform device. Situations might arise, however, where it may be useful to be able to detect if a device supports freeform multi-window mode. Fortunately, this can be achieved by checking for the freeform window management feature in the package manager. The following code example checks for freeform multi-window support and returns a Boolean value based on the result of the test:

fun checkFreeform(): Boolean {

    return packageManager.hasSystemFeature(

            PackageManager.FEATURE_FREEFORM_WINDOW_MANAGEMENT)

}

71.6 Enabling Multi-Window Support in an App

The android:resizableActivity manifest file setting controls whether multi-window behavior is supported by an app. This setting can be made at either the application or individual activity levels. The following fragment, for example, configures the activity named MainActivity to support both split-screen and freeform multi-window modes:

<activity

    android:name=".MainActivity"

    android:resizeableActivity="true"

    android:label="@string/app_name"

    android:theme="@style/AppTheme.NoActionBar">

    <intent-filter>

        <action android:name="android.intent.action.MAIN" />

 

        <category android:name="android.intent.category.LAUNCHER" />

    </intent-filter>

</activity>

Setting the property to false will prevent the activity from appearing in split-screen or freeform mode and the corresponding buttons will not appear in the Overview menu for the app. Selecting an activity for which multi-window support is disabled as the second activity in a split-screen configuration will result in a message appearing indicating that the app may not support multi-window mode.

71.7 Specifying Multi-Window Attributes

A number of attributes are available as part of the <layout> element for specifying the size and placement of an activity when it is launched into a multi-window mode. The initial height, width and position of an activity when launched in freeform mode may be specified using the following attributes:

android:defaultWidth – Specifies the default width of the activity.

android:defaultHeight – Specifies the default height of the activity.

android:gravity – Specifies the initial position of the activity (start, end, left, right, top etc.).

Note that the above attributes apply to the activity only when it is displayed in freeform mode. The following example configures an activity to appear with a specific height and width at the top of the starting edge of the screen:

<activity android:name=".MainActivity ">

    <layout android:defaultHeight="350dp"

          android:defaultWidth="450dp"

          android:gravity="start|end" />

</activity>

The following <layout> attributes may be used to specify the minimum width and height to which an activity may be reduced in either split-view or freeform modes:

android:minimalHeight – Specifies the minimum height to which the activity may be reduced while in split-screen or freeform mode.

android:minimalWidth - Specifies the minimum width to which the activity may be reduced while in split-screen or freeform mode.

When the user slides the split-screen divider beyond the minimal height or width boundaries, the system will stop resizing the layout of the shrinking activity and simply clip the user interface to make room for the adjacent activity.

The following manifest file fragment implements the minimal width and height attributes for an activity:

<activity android:name=".MainActivity ">

    <layout android:minimalHeight="400dp"

          android:minimalWidth="290dp" />

</activity>

71.8 Detecting Multi-Window Mode in an Activity

Situations may arise where an activity needs to detect whether it is currently being displayed to the user in multi-window mode. The current status can be obtained via a call to the isInMultiWindowMode() method of the Activity class. When called, this method returns a true or false value depending on whether or not the activity is currently full screen:

if (this.isInMultiWindowMode()) {

    // Activity is running in Multi-Window mode

} else {

    // Activity is not in Multi-Window mode

}

71.9 Receiving Multi-Window Notifications

An activity will receive notification that it is entering or exiting multi-window mode if it overrides the onMultiWindowModeChanged() callback method. The first argument passed to this method is true on entering multi-window mode, and false when the activity exits the mode. The new configuration settings are contained within the Configuration object passed as the second argument:

override fun onMultiWindowModeChanged(isInMultiWindowMode: Boolean,

                                newConfig: Configuration?) {

    super.onMultiWindowModeChanged(isInMultiWindowMode, newConfig)

    

    if (isInMultiWindowMode) {

        // Activity has entered multi-window mode

    } else {

        // Activity has exited multi-window mode

    }

}

As outlined in the chapter entitled “Handling Android Activity State Changes”, Android 10 and later allow multiple activities to be in the resumed state simultaneously (otherwise known as multi-resume). The activity with which the user most recently interacted is referred to as the topmost resumed activity. To track an activity as it gains and loses topmost resumed status, the onTopResumedActivityChanged() callback method may be implemented within the activity, for example:

override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {

    super.onTopResumedActivityChanged(isTopResumedActivity)

 

    if (isTopResumedActivity) {

        // Activity is now topmost resumed activity

    } else {

        // Activity is no longer topmost resumed activity

    }

}

It may also be possible to take advantage of multi-resume in an app on some devices running Android 9 by enabling the following property in the app manifest file:

<meta-data

android:name="android.allow_multiple_resumed_activities" android:value="true" />

71.10 Launching an Activity in Multi-Window Mode

In the “Android Explicit Intents – A Worked Example” chapter of this book, an example app was created in which an activity uses an intent to launch a second activity. By default, activities launched via an intent are considered to reside in the same task stack as the originating activity. An activity can, however, be launched into a new task stack by passing through the appropriate flags with the intent.

When an activity in multi-window mode launches another activity within the same task stack, the new activity replaces the originating activity within the split-screen or freeform window (the user returns to the original activity via the back button).

When launched into a new task stack in split-screen mode, however, the second activity will appear in the window adjacent to the original activity, allowing both activities to be viewed simultaneously. In the case of freeform mode, the launched activity will appear in a separate window from the original activity.

In order to launch an activity into a new task stack, the following flags must be set on the intent before it is started:

Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT

Intent.FLAG_ACTIVITY_MULTIPLE_TASK

Intent.FLAG_ACTIVITY_NEW_TASK

The following code, for example, configures and launches a second activity designed to appear in a separate window:

val i = Intent(this, SecondActivity::class.java)

 

i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT or

        Intent.FLAG_ACTIVITY_MULTIPLE_TASK or

        Intent.FLAG_ACTIVITY_NEW_TASK)

 

startActivity(i)

71.11 Configuring Freeform Activity Size and Position

By default, an activity launched into a different task stack while in freeform mode will be positioned in the center of the screen at a size dictated by the system. The location and dimensions of this window can be controlled by passing launch bounds settings to the intent via the ActivityOptions class. The first step in this process is to create a Rect object configured with the left (X), top (Y), right (X) and bottom (Y) coordinates of the rectangle representing the activity window. The following code, for example, creates a Rect object in which the top-left corner is positioned at coordinate (0, 0) and the bottom-right at (100, 100):

val rect = Rect(0, 0, 100, 100)

The next step is to create a basic instance of the ActivityOptions class and initialize it with the Rect settings via the setLaunchBounds() method:

val options = ActivityOptions.makeBasic()

val bounds = options.setLaunchBounds(rect)

Finally, the ActivityOptions instance is converted to a Bundle object and passed to the startActivity() method along with the Intent object:

startActivity(i, bounds.toBundle())

Combining these steps results in a code sequence that reads as follows:

val i = Intent(this, SecondActivity::class.java)

i.addFlags(Intent.FLAG_ACTIVITY_LAUNCH_ADJACENT or

        Intent.FLAG_ACTIVITY_MULTIPLE_TASK or

        Intent.FLAG_ACTIVITY_NEW_TASK)

 

val rect = Rect(0, 0, 100, 100)

 

val options = ActivityOptions.makeBasic()

val bounds = options.setLaunchBounds(rect)

 

startActivity(i, bounds.toBundle())

When the second activity is launched by the intent while the originating activity is in freeform mode, the new activity window will appear with the location and dimensions defined in the Rect object.

71.12 Summary

Android 7 introduced multi-window support, a system whereby more than one activity is displayed on the screen at any one time. This feature now forms the foundation of providing support for foldable devices. The three modes provided by multi-window support are split-screen, freeform and picture-in-picture. In split-screen mode, the screen is split either horizontally or vertically into two panes with an activity displayed in each pane. Freeform mode, which is only supported on certain Android devices, allows each activity to appear in a separate, movable and resizable window. As outlined in this chapter, a number of methods and property settings are available within the Android SDK to detect, respond to and control multi-window behavior within an app.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.239.9.151