Lifecycle-Aware Components Live Data ViewModel Room Library

Similar documents
Activities and Fragments

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

States of Activities. Active Pause Stop Inactive

Overview of Activities

Tablets have larger displays than phones do They can support multiple UI panes / user behaviors at the same time

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

LECTURE NOTES OF APPLICATION ACTIVITIES

Lifecycle Callbacks and Intents

CMSC436: Fall 2013 Week 3 Lab

Minds-on: Android. Session 2

Android Fundamentals - Part 1

Android Basics. Android UI Architecture. Android UI 1

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

Activities. Repo:

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Understanding Application

UNDERSTANDING ACTIVITIES

University of Babylon - College of IT SW Dep. - Android Assist. Lect. Wadhah R. Baiee Activities

CS 4518 Mobile and Ubiquitous Computing Lecture 4: Data-Driven Views, Android Components & Android Activity Lifecycle Emmanuel Agu

CS378 - Mobile Computing. Anatomy of an Android App and the App Lifecycle

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Programming Mobile Applications with Android Lab2

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

Lab 1: Getting Started With Android Programming

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

EMBEDDED SYSTEMS PROGRAMMING Application Basics

Programming with Android: Android for Tablets. Dipartimento di Scienze dell Informazione Università di Bologna

Understanding and Detecting Wake Lock Misuses for Android Applications

Fragments and the Maps API

Understanding and Detecting Wake Lock Misuses for Android Applications

Mobile Computing Fragments

CS 528 Mobile and Ubiquitous Computing Lecture 4a: Fragments, Camera Emmanuel Agu

Google Maps Troubleshooting

UI Fragment.

Introduction to Android development

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Android Ecosystem and. Revised v4presenter. What s New

Getting Started. Dr. Miguel A. Labrador Department of Computer Science & Engineering

CS378 -Mobile Computing. Anatomy of and Android App and the App Lifecycle

Static Detection of Energy Defect Patterns in Android Applications

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

Android Activities. Akhilesh Tyagi

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Application Fundamentals

PILOT FM Radio SDK AUGUST 31, 2016

Have a development environment in 256 or 255 Be familiar with the application lifecycle

application components

Repairing crashes in Android Apps. Shin Hwei Tan Zhen Dong Xiang Gao Abhik Roychoudhury National University of Singapore

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (Part 1-2) Hello-Goodbye App Tutorial

Lecture 2 Android SDK

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Mobile Computing. Introduction to Android

2017 Pearson Educa2on, Inc., Hoboken, NJ. All rights reserved.

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

COLLEGE OF ENGINEERING, NASHIK-4

Libraries are wri4en in C/C++ and compiled for the par>cular hardware.

Vienos veiklos būsena. Theory

CS 528 Mobile and Ubiquitous Computing Lecture 3: Android UI, WebView, Android Activity Lifecycle Emmanuel Agu

Static Analysis for Android: GUIs, Callbacks, and Beyond

ANDROID MOCK TEST ANDROID MOCK TEST IV

CE881: Mobile & Social Application Programming

Embedded Systems Programming - PA8001

CPET 565 Mobile Computing Systems CPET/ITC 499 Mobile Computing. Lab & Demo 2 (1 &2 of 3) Hello-Goodbye App Tutorial

Real-Time Embedded Systems

Computer Science E-76 Building Mobile Applications

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

EMDK for Android Workshops Prashanth Kadur

Introduction to Android

CS 4330/5390: Mobile Application Development Exam 1

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

INTRODUCTION TO ANDROID

Android Application Development

Summarizing Control Flow of Callbacks for Android API Methods

Introduction to Mobile Application Development Using Android Week Four Video Lectures

Fragments. Fragments may only be used as part of an ac5vity and cannot be instan5ated as standalone applica5on elements.

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

CS 193A. Activity state and preferences

OFFLINE MODE OF ANDROID

Android Programmierung leichtgemacht. Lars Vogel

COSC 3P97 Mobile Computing

Leak Canary Intro Jennifer McGee

Android framework overview Activity lifecycle and states

Services. service: A background task used by an app.

Introduction to Android

CS378 -Mobile Computing. Services and Broadcast Receivers

Spring Lecture 5 Lecturer: Omid Jafarinezhad

Android Application Development using Kotlin

Institutionen för datavetenskap. Effects on performance and usability for cross-platform application development using React Native

EMBEDDED SYSTEMS PROGRAMMING UI and Android

Comp 595MC/L: Mobile Computing CSUN Computer Science Department Fall 2013 Midterm

Testing Mobile Apps CS 4720 Mobile Application Development

Programming with Android: System Architecture. Dipartimento di Scienze dell Informazione Università di Bologna

Midterm Examination. CSCE 4623 (Fall 2017) October 20, 2017

Services. Background operating component without a visual interface Running in the background indefinitely

Static Analysis for Android: GUIs, Callbacks, and Beyond

Mobile User Interfaces

Mobile Application Development MyRent Settings

Android. Mobile operating system developed by Google A complete stack. Based on the Linux kernel Open source under the Apache 2 license

Transcription:

Lifecycle-Aware Components Live Data ViewModel Room Library

Multiple entry points launched individually Components started in many different orders Android kills components on reconfiguration / low memory

Don t store app data or state in your app components Don t design your app components so they depend on each other

Ties app components to lifecycle events LifeCycle Represents Android lifecycle LifecycleOwner A component with an Android lifecycle LifecycleObserver Callbacks for listening to lifecycle changes

Holds information about the lifecycle state of an Android component State Enum representing lifecyle states Events Enum representing lifecycle events (transitions between states)

CREATED - Created state for LifecycleOwner DESTROYED - Destroyed state for LifecycleOwner INITIALIZED - Initialized state for LifecycleOwner RESUMED - Resumed state for LifecycleOwner STARTED - Started state for LifecycleOwner

ON_ANY - Constant matching all events ON_CREATE - oncreate event of the LifecycleOwner ON_DESTROY - ondestroy event of the LifecycleOwner ON_PAUSE - onpause event of the LifecycleOwner ON_RESUME - onresume event of the LifecycleOwner ON_START - onstart event of the LifecycleOwner ON_STOP - onstop event of the LifecycleOwner

void addobserver(lifecycleobserver observer) Adds a LifecycleObserver that will be notified when the LifecycleOwner changes state void removeobserver(lifecycleobserver observer) Removes the given observer from the observers list Lifecycle.State getcurrentstate() Returns the current state of the Lifecycle

Represents a component with an Android lifecycle An interface that returns a Lifecycle object from the getlifecycle() method

Callbacks for listening to lifecycle changes to a LifecycleOwner Our examples will use Java 8 Observe events with DefaultLifecycleObserver Add "android.arch.lifecycle:common-java8:<version>" to build.gradle file

void oncreate(lifecycleowner owner) Notifies that ON_CREATE event occurred. void onstart(lifecycleowner owner) Notifies that ON_START event occurred. void onresume(lifecycleowner owner) Notifies that ON_RESUME event occurred.

void ondestroy(lifecycleowner owner) Notifies that ON_DESTROY event occurred void onpause(lifecycleowner owner) Notifies that ON_PAUSE event occurred void onstop(lifecycleowner owner) Notifies that ON_STOP event occurred

ON_CREATE, ON_START, ON_RESUME events are dispatched after the LifecycleOwner's related method returns ON_PAUSE, ON_STOP, ON_DESTROY events are dispatched before the LifecycleOwner's related method is called

Responsible for managing data for an Activity or a Fragment Handles communication between the Activity or Fragment and the rest of the application

Associated with a scope (a Fragment or an Activity) Retained as long as the scope is alive Will not be destroyed if its owner is destroyed for a configuration change The new instance of the owner will reconnected to the existing ViewModel

Should never access the view hierarchy or hold a reference to the Activity or the Fragment

void oncleared() This method will be called when this ViewModel is no longer used and will be destroyed

static ViewModelProvider of (FragmentActivity activity) Creates a ViewModelProvider, which retains ViewModels while a scope of given Activity is alive

Data holder observeable within a given lifecycle Observer paired with a LifecycleOwner Observer notified when data changes only if the LifecycleOwner is in active state LifecycleOwner is considered active, if its state is STARTED or RESUMED Designed to hold individual data fields of ViewModel Can also be used to share data between components

LifecycleAware Ticker

Examine Source Code

Keep your UI controllers (activities and fragments) as lean as possible. They should not try to acquire their own data; instead, use a ViewModel to do that, and observe the LiveData to reflect the changes back to the views Try to write data-driven UIs where your UI controller s responsibility is to update the views as data changes, or notify user actions back to theviewmodel Put your data logic in your ViewModel class. ViewModel should serve as the connector between your UI controller and the rest of your application Never reference a View or Activity context in your ViewModel. If the ViewModel outlives the activity (in case of configuration changes), your activity will be leaked and not properly garbage-collected

Cloud/Firebase