Slide 1

Slide 1 text

Android Latest Versions & Architecture Components. GAURAV BHATNAGAR

Slide 2

Slide 2 text

Agenda • What’s new in Android P • New features integrated in Android 8.0 & 8.1 (Oreo) • Android Jetpack • Details about Architecture Components • Lifecycle Component • LiveData • ViewModel • Room • Paging • WorkManager & Navigation Components

Slide 3

Slide 3 text

What’s new in Android P • Slices • Biometric Prompt. • Foreground Service Permission + Improvements on Background processing using Priority Buckets • HTTPS Required by default. • Notification Improvements • Display Cutouts. • AppComponentFactory. • WebView Improvements Source : https://developer.android.com/preview/

Slide 4

Slide 4 text

Slices • A “slice” is a mechanism to show your app’s highlight inside of Google Search/Assistant. • Here the actual UI is rendered by another app. • Slice is built on the interaction between provider app and the host app. Source : https://goo.gl/ya9HpA

Slide 5

Slide 5 text

Slice Internals Source : https://goo.gl/AqfsvJ

Slide 6

Slide 6 text

BiometricPrompt • The FingerprintManager API has been deprecated and replaced with BiometricPrompt. • It provides stock UI around fingerprint scanning. • BiometricPrompt can handle other forms of biometric authentication such as face scanning or iris scanning.

Slide 7

Slide 7 text

Priority Buckets • Active • Working Set • Frequent • Rare • Never Source : https://goo.gl/9d4tys

Slide 8

Slide 8 text

App Security Improvements • From the second half of 2018 Google Play will require new apps & app updates target the recent Android API level. • Aug 2018 : New apps required to target API level 26 (Android 8.0) or higher • November 2018 : Updates to existing apps required to target API level 26 or higher. • 2019 onwards : Each year the targetSdkVersion requirement will advance.

Slide 9

Slide 9 text

Android Oreo • Notification Dots & Channels • Autofill Framework. • Adaptive Icons • Background Limitations. • Java 8 language APIs • Picture-in-picture • Downloadable fonts. • Autosizing TextView • Neural Networks API and many more. Source : https://developer.android.com/about/versions/oreo/

Slide 10

Slide 10 text

Notification Channels & Badges • Objectives • Visual Consistency • Expand Animation. • Providing more control to the end users. • Avoiding all the spam notifications. • Implementations • To target Oreo need to implement notification channels and incorporate badges. Source :https://goo.gl/ZZLaaD

Slide 11

Slide 11 text

Background Processing Limitations • Improve RAM / Battery performance. • Restricting Background processes for different applications • Services run freely in foreground • Background services are stopped when idle. • Bound services are not affected. • Limitations on Background Location Updates • When in Background apps to receive location updates only a few times each hour.

Slide 12

Slide 12 text

Background Processing Limitations Source :https://goo.gl/DuVrmb

Slide 13

Slide 13 text

Android Jetpack Components • A collection of libraries that are individually adoptable and built to work together to make android developers more productive. • Accelerate Development • Eliminates boilerplate code. • Build high quality apps with no memory leaks and fewer crashes with backward compatibility integrated.

Slide 14

Slide 14 text

Android Jetpack Source : https://developer.android.com/jetpack/docs/guide

Slide 15

Slide 15 text

Need of Architecture Components • Achieve Separation of Concerns • Always Drive UI from the Model. • Try to implement the architecture which is robust , testable and easily maintainable. • Now it is 1.x (Stable) out of its alpha and beta stage.

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Final Architecture Source : https://developer.android.com/jetpack/docs/guide

Slide 18

Slide 18 text

Life-Cycle Aware Components • Components that can automatically adjust their behaviour based on the current lifecycle state of an activity or fragment. • They are core to how android framework works and our app must respect them. • Using the LifecycleOwner Interface and LifecycleObserver in an optimized manner. • For e.g. If we need to implement the location listener for live tracking in our application we can make the location listener implement LifecycleObserver and initialize in our UI component’s (Activity) onCreate().

Slide 19

Slide 19 text

Using the LifecycleOwner & LifecycleObserver class MyActivity extends AppCompatActivity { private MyLocationListener myLocationListener; public void onCreate(...) { myLocationListener = new MyLocationListener(this, getLifecycle(), location -> { // update UI }); Util.checkUserStatus(result -> { if (result) { myLocationListener.enable(); } }); } } class MyLocationListener implements LifecycleObserver { private boolean enabled = false; public MyLocationListener(Context context, Lifecycle lifecycle, Callback callback) { ... } @OnLifecycleEvent(Lifecycle.Event.ON_START) void start() { if (enabled) { // connect } } public void enable() { enabled = true; if (lifecycle.getCurrentState().isAtLeast(STARTED)) { // connect if not connected } } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) void stop() { // disconnect if connected } } Source : https://developer.android.com/topic/libraries/architecture/lifecycle

Slide 20

Slide 20 text

Event & State Source : https://developer.android.com/topic/libraries/architecture/lifecycle

Slide 21

Slide 21 text

Best Practices (Handling Lifecycle) • Keeping the UI as lean as possible. • Since the UI will be dumb, it will be easier to test using Instrumentation tests. • Components that implement LifecycleObserver work seamlessly with components that implement LifecycleOwner because an owner can provide a lifecycle, which an observer can register to watch. • Using this in video buffering, network connectivity , animated drawable etc.

Slide 22

Slide 22 text

LiveData • It is an observable data holder class that notifies views when the underlying model changes. The USP is that it is lifecycle aware. • The awareness makes LiveData quite invaluable as it makes sure that UI components are updated only in an active lifecycle state. Source : https://goo.gl/ff4NK9

Slide 23

Slide 23 text

Using Observer pattern in UI with callbacks in data layer Source : https://goo.gl/ff4NK9

Slide 24

Slide 24 text

LiveData in repositories Source : https://goo.gl/ff4NK9

Slide 25

Slide 25 text

Advantages of LiveData • Ensure that Lean UI matches data layer state. • Minimizes memory leaks. • Reduces the crash rates and improves stability. • Proper Configuration Changes. • No more manual life-cycle handling. • Always up-to date data. • Sharing Source : https://goo.gl/ff4NK9

Slide 26

Slide 26 text

ViewModel • It is designed to hold and manage UI-related data in a life-cycle conscious way. This allows data to survive configuration changes such as screen rotations or device language modifications. • A ViewModel outlives the specific Activity or Fragment instances. • A ViewModel will stay in memory until the Lifecycle it’s scoped to goes away permanently – in the case of an Activity, once it finishes; in the case of a Fragment, once it’s detached. • It shouldn’t reference any Views directly inside of it or hold reference to a context. This can cause memory leaks. • ViewModelProvider instance is tied to either an Activity(AppCompatActivity) or Fragment.

Slide 27

Slide 27 text

ViewModel Lifecycle Source : https://goo.gl/vaK5oZ

Slide 28

Slide 28 text

Loading Data with ViewModel Source : https://developer.android.com/topic/libraries/architecture/viewmodel

Slide 29

Slide 29 text

Room (Persistence Library) • Provides an abstraction layer over SQLite Database. • Optimizes database access while harnessing full power of SQLite. • It serves as app’s single source of truth which assists in providing offline first experience to end users.

Slide 30

Slide 30 text

Room architecture Diagram • There are three major components in Room: • Entity • DAO (Data Access Objects) • Database Source : https://goo.gl/MEbi8j

Slide 31

Slide 31 text

NetworkBoundResource Source : https://developer.android.com/jetpack/docs/guide

Slide 32

Slide 32 text

References • https://developer.android.com/jetpack/docs/guide • https://developer.android.com/topic/libraries/architecture/ • https://developer.android.com/topic/libraries/architecture/lifecycle • https://developer.android.com/topic/libraries/architecture/viewmodel • https://medium.com/google-developers/viewmodels-and-livedata-patterns-antipatterns-21efaef74a54 • https://riggaroo.co.za/android-architecture-components-looking-viewmodels-part-2/ • https://medium.com/google-developers/viewmodels-a-simple-example-ed5ac416317e • https://developer.android.com/jetpack/ • https://android-developers.googleblog.com/2018/05/use-android-jetpack-to-accelerate-your.html

Slide 33

Slide 33 text

References • https://medium.com/@ajaysaini.official/building-database-with-room-persistence-library-ecf7d0b8f3e9 • https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo- ab384762a66c • https://medium.com/exploring-android/exploring-android-o-notification-badges-32e1152eb1a0 • https://medium.com/google-developer-experts/exploring-android-p-priority-buckets-d34d12059d36 • https://blog.novoda.com/android-p-slices-missing-documentation-part-2/ • https://developer.android.com/about/versions/oreo/ • https://developer.android.com/about/versions/oreo/android-8.0-migration • https://developer.android.com/preview/features