For the More Curious: UI Updates and Multi-Window Mode

Prior to Android 7.0 Nougat, most activities spent very little time in the paused state. Instead, activities passed through the paused state quickly on their way to either the resumed state or the stopped state. Because of this, many developers assumed they only needed to update their UI when their activity was in the resumed state. It was common practice to use onResume() and onPause() to start or stop any ongoing updates related to the UI (such as animations or data refreshes).

When multi-window mode was introduced in Nougat, it broke the assumption that resumed activities were the only fully visible activities. This, in turn, broke the intended behavior of many apps. Now, paused activities can be fully visible for extended periods of time when the user is in multi-window mode. And users will expect those paused activities to behave as if they were resumed.

Consider video, for example. Suppose you have a pre-Nougat app that provides simple video playback. You start (or resume) video playback in onResume() and pause playback in onPause(). Multi-window mode comes along, but your app stops playback when it is paused and users are interacting with another app in the second window. Users start complaining, because they want to watch their videos while they send a text message in a separate window.

Luckily, the fix is relatively simple: Move your playback resuming and pausing to onStart() and onStop(). This goes for any live-updating data, like a photo gallery app that refreshes to show new images as they are pushed to a Flickr stream (as you will see later in this book).

In short, in the post-Nougat world, your activities should update the UI during their entire visible lifecycle, from onStart() to onStop().

Unfortunately, not everyone got the memo, and many apps still misbehave in multi-window mode. To fix this, the Android team introduced a spec to support multi-resume for multi-window mode in November 2018. Multi-resume means that the fully visible activity in each of the windows will be in the resumed state when the device is in multi-window mode, regardless of which window the user last touched.

You can opt in to using multi-resume mode for your app when it is running on Android 9.0 Pie by adding metadata to your Android manifest: <meta-data android:name="android.allow_multiple_resumed_activities" android:value="true" />. You will learn more about the manifest in Chapter 6.

Even if you opt in, multi-resume will only work on devices whose manufacturer implemented the multi-resume spec. And, as of this writing, no devices on the market implement it. However, there are rumors that the spec will become mandatory in the next version of Android (the yet-to-be-named Android Q).

Until multi-resume becomes a readily available standard across most devices in the marketplace, use your knowledge of the activity lifecycle to reason about where to place UI update code. You will get a lot of practice doing so throughout the course of this book.

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

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