User Interface

The user interface is certainly the most important part of any end-user app. For corporate usage, apps without user interfaces are possible, but even then in most cases you will have some kind of rudimentary UI, if for no other reason just to avoid the An

  • PDF / 546,059 Bytes
  • 70 Pages / 504 x 720 pts Page_size
  • 81 Downloads / 229 Views

DOWNLOAD

REPORT


9

User Interface The user interface is certainly the most important part of any end-user app. For corporate usage, apps without user interfaces are possible, but even then in most cases you will have some kind of rudimentary UI, if for no other reason just to avoid the Android OS killing your app too readily during resource housekeeping tasks. In this chapter, we will not cover the basics of UI development for Android. Instead, it is expected that you’ve read some introductory-level book or worked through the official tutorials (or any number of the other tutorials you will find on the Web). What we do here is to cover a couple of important UI-related issues that help you to create more stable apps or apps with special outstanding requirements.

Background Tasks Android relies on a single-threaded execution model. This means when an app starts, it by default starts only a single thread, called the main thread, in which all actions run unless you explicitly use background threads for certain tasks. This automatically means you have to take special precautions if you have long-running tasks that would interrupt a fluent UI workflow. It is not acceptable for modern apps to have the UI freeze after the user taps a button or because this action leads to a process running for a few seconds or longer. It is therefore vital to put long-running tasks into the background. One way to accomplish putting things into the background is to use IntentService objects, as described in Chapter 4. Depending on the circumstances, it might, however, blow up your app design to put all background work into services; in addition, having too many services run on a device will not help keep resources usage low. Another option is to use loaders as described in Chapter 8. For low-level tasks, however, it is better to use a more low-level approach. You have several options here, which we describe in the following sections.

© Peter Späth 2018 P. Späth, Pro Android with Kotlin, https://doi.org/10.1007/978-1-4842-3820-2_9

191

192

CHAPTER 9: User Interface

Java Concurrency At a low level, you can use Java threads and classes from the java.util.concurrent package to handle background jobs. Beginning with Java 7, those classes have become quite powerful, but you need to fully understand all options and implications. You will quite often read that directly handling threads from inside the Android OS is not a good idea because threads are expensive when speaking of system resources. While this was certainly true for older devices and old Android versions, nowadays this is just no longer the case. For me, a simple test on a Motorola Moto G4 starting 1,000 threads and waiting until all are running took approximately 0.0006 seconds per thread. So, if you are used to Java threads and less than a millisecond for the thread to start is good for you, there is no performance reason for not using Java threads. However, you must take into account that threads run outside any Android component lifecycle, so you have to handle lifecycle issues manually if yo