Migrating to Android 10: When You've Had Too Much Dessert

Android 10 is out! 🎉

The stable version of Android 10 was released by Google on September 3rd, 2019 and with it, came along a whole slew of new features for Android users. 🥳

In this article, we’re going to briefly explore what’s new for developers and how they can leverage the new Android 10 features and APIs to support their existing apps, and smoothly transition into the new Android operating system.

What’s with the name, Android 10?

As you may have noticed, Android 10 is named, well… Android 10. It’s not named after a dessert or a sweet, as Android versions usually are.

While the dessert names satisfied our sweet tooth, Google has decided to keep it simple with numbers, as more often than not, the names weren’t really understood by everyone in the global community. Also, new users found it hard to keep track of the version sequences.

“Lollipop is the one that came after KitKat, right?”

“So, you’re saying that there are two versions of Lollipop?”

“Is it pronounced Now-gat or Noo-jat?”

So, following this confusion, Google decided that all Android versions from here on are just gonna be plain old numbers.

What’s new in Android 10?

Let’s talk briefly about the new features, APIs, and changes that Android 10 has brought:

5G Networks

Android10 Migration 5g network

Android 10 supports 5G to have super-fast connectivity with lower latency. There are also some new classes in place to extend the support of 5G to developers.

This means that developers can now check if a phone has 5G connectivity with the help of TelephonyManager. You can find the extended package summary here.

Let’s look at a basic code snippet of how to check for 5G connectivity:

    val telephonyManager = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager

    // Requires the permission ACCESS_COARSE_LOCATION
    val cellInfo = telephonyManager.allCellInfo[0]

    // CellInfoNr is the newly-introduced class for 5G connectivity
    if (cellInfo is [CellInfoNr](https://developer.android.com/reference/android/telephony/CellInfoNr.html)) {
        // Device has 5G connectivity
        doSomething()
    }

Foldables & Multi-Display Support

Source: [Android Developers](https://developer.android.com/guide/topics/ui/foldables)Source: Android Developers

Android 10 brings in more support for foldable devices and secondary displays.

When developing for foldable devices, developers should make use of the onResume() and onPause() methods to gracefully handle the UI state changes, two methods in the Activity lifecycle to which Android 10 brings in more improvements for foldables.

Android 10 also supports a wider variety of screen aspect ratios, since foldable devices can vary from vendor to vendor. Here's the recommended list of aspect ratios you need to test your app:

Source: [Android Developers](https://developer.android.com/guide/topics/ui/foldables)Source: Android Developers

Up until Android 9.0 (Pie), if you had two apps running side-by-side in your phone, only the app in focus was in the resumed state. The other app was in the paused state. This can cause problems if you need something to keep happening in the other app, even if it is paused and out of focus.

With Android 10, you can make sure your other app is also in resumed state by adding this meta-data tag inside the application tag in your manifest file:

    <application>
    ...
      <meta-data
        android:name="android.allow_multiple_resumed_activities"
        android:value="true" />
    ...
    </application>

This feature is called multi-resume and it means that you can have multiple apps in the resumed state. It also brings a new lifecycle callback called onTopResumedActivityChanged(), which you can read about here.

Android 8.0 brought support for multiple displays, but Android 10 extends that by supporting multi-resume for multiple activities of the same app on multiple displays. The newly added getDisplayContext() method that provides the Context of the current display for developers to load resources from.

You can read up on how to use the multi-display feature here.

Smart Reply via Notifications

Source: [Android Developers](https://developer.android.com/about/versions/10/highlights)Source: Android Developers

The on-device ML model works with Android 10 and suggests contextual actions and smart replies right in the notifications itself. These contextual actions could include intents to other apps, like opening a location via Google Maps, that you get in a text.

This is one really cool feature where you, as a developer, don’t really have to do anything to get it in your app. It’s just there by default on all devices in Android 10.

But if you want to disable this feature, you can call setAllowGeneratedReplies(false) on your Notification.Action.Builder, and setAllowSystemGeneratedContextualActions(false) on your Notification.Builder.

Force Dark

Source: [Android Developers](https://developer.android.com/about/versions/10/highlights)Source: Android Developers

With previous versions of Android, developers have had to define a DayNight theme that extends Theme.AppCompat.DayNight or Theme.MaterialComponents.DayNight in your styles.xml file.

But with Android 10, you can force a dark theme right away without having to even define an explicit style for your dark theme. This is known as Force Dark.

To do this, you just need to set forceDarkAllowed to true in your app’s theme:

    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        
        <item name="android:forceDarkAllowed">true</item>
    </style>

You can also disable/enable Force Dark on specific views too:

    myTextView.isForceDarkAllowed = false
    myOtherTextView.isForceDarkAllowed = true

Keep in mind that Force Dark doesn’t work if your app already uses a dark theme (Theme.Material or Theme.AppCompat) or a DayNight theme. Also be sure to test your app thoroughly if you’re using Force Dark.

Read more about Force Dark here.

Gesture Navigation

Gesture navigation is something that you might have noticed in the iPhone XR and iPhone XS, and it’s coming to Android 10 too!

Gestural navigation eliminates the need for the navigation bar buttons. Users can swipe up to close the app or swipe right to go back to the previous screen. To take better advantage of this feature, I’d recommend setting the following values in your styles.xml file:

    <!-- values-29/styles.xml: -->
    
    <style name="AppTheme" parent="...">
        <item name="android:navigationBarColor">@android:color/transparent</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

To lay out your app in fullscreen mode, set the system UI visibility as follows:

    window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

Read more about how to handle gesture insets here.

Settings Panel API

With Android 10, developers can now use the Settings Panel API to show a floating UI on top of their running app to let users get quick access to common settings like Internet Connectivity, NFC, Wi-Fi and Volume.

Using this is pretty simple: You just fire off an Intent with one of the actions from the Settings Panel API:

    val intent = Intent([ACTION_WIFI](https://developer.android.com/reference/android/provider/Settings.Panel.html#ACTION_WIFI))
    startActivity(intent)

This shows a floating UI which lets the users change Wi-Fi settings.

Background Location Permission

Starting with Android 10, if your app needs your user’s location even when the app is in the background, you’ll need to include the new ACCESS_BACKGROUND_LOCATION permission in your app:

    <manifest ... >
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      **<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />**
    </manifest>

You should also request for this permission during run-time just like you’d normally request for other dangerous permissions. Upon requesting this permission, the user is show the following dialog:

Source: [Android Developers](https://developer.android.com/about/versions/10/highlights)Source: Android Developers

This allows users to:

  • Give your app permission to use the location all the time, even in the background

  • Give your app permission to use the location only when the app is in use

  • Deny location permission altogether

You can read more about these location permission changes here.

Conclusion

This covers the major changes and features in Android 10. There are a handful of other security and behaviour changes as well.

Make your app Android 10-friendly today and keep your users happy. 😁

Read up more about all the changes here:

Android 10 for Developers | Android Developers

Last updated on Sep 20, 2019