Slide 1

Slide 1 text

Nishant Srivastava The A,B and C of Lifecycle Components

Slide 2

Slide 2 text

Nishant Srivastava twitter.com/nisrulz github.com/nisrulz www.nisrulz.com

Slide 3

Slide 3 text

Soundbrenner

Slide 4

Slide 4 text

The Challenge @nisrulz #DCBERLIN18

Slide 5

Slide 5 text

The Challenge Android activity lifecycle.. @nisrulz #DCBERLIN18

Slide 6

Slide 6 text

The Challenge Android activity lifecycle @nisrulz #DCBERLIN18

Slide 7

Slide 7 text

The Challenge Android activity lifecycle - Many states @nisrulz #DCBERLIN18

Slide 8

Slide 8 text

The Challenge Android activity lifecycle - Many states - Repeats on configuration changes @nisrulz #DCBERLIN18

Slide 9

Slide 9 text

The Challenge Android activity lifecycle - Many states - Repeats on configuration changes Not to forget... @nisrulz #DCBERLIN18

Slide 10

Slide 10 text

The Challenge ...the Fragment lifecycle. @nisrulz #DCBERLIN18

Slide 11

Slide 11 text

The Challenge ...the Fragment lifecycle. @nisrulz #DCBERLIN18

Slide 12

Slide 12 text

Architecture Components @nisrulz #DCBERLIN18

Slide 13

Slide 13 text

Lifecycle Components @nisrulz #DCBERLIN18

Slide 14

Slide 14 text

Lifecycle Components Series of classes designed to help deal with lifecycles in a more consistent fashion. @nisrulz #DCBERLIN18

Slide 15

Slide 15 text

Lifecycle Components Series of classes designed to help deal with lifecycles in a more consistent fashion. - Lifecycle - LifecycleOwner - LifecycleObserver @nisrulz #DCBERLIN18

Slide 16

Slide 16 text

Lifecycle @nisrulz #DCBERLIN18

Slide 17

Slide 17 text

Lifecycle Series of states an object can be in. For Android: Object that defines Android lifecycle states. i.e. Activity & Fragment @nisrulz #DCBERLIN18

Slide 18

Slide 18 text

Lifecycle @nisrulz #DCBERLIN18

Slide 19

Slide 19 text

Lifecycle dependencies { def lifecycle_version = "1.1.1" // Support library already depends on this (since v26.1.0) // Both AppCompatActivity & Support Fragment implement // LifecycleOwner interface. // Lifecycles only implementation "android.arch.lifecycle:runtime:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 20

Slide 20 text

Lifecycle // Source code public abstract class Lifecycle { // Adds a LifecycleObserver addObserver(@NonNull LifecycleObserver observer) // Removes the given observer from the observers list removeObserver(@NonNull LifecycleObserver observer) // Current state of the Lifecycle getCurrentState() // Compares the lifecycle states isAtLeast(@NonNull State state) ... } @nisrulz #DCBERLIN18

Slide 21

Slide 21 text

Lifecycle // Source code public abstract class Lifecycle { // Adds a LifecycleObserver addObserver(@NonNull LifecycleObserver observer) // Removes the given observer from the observers list removeObserver(@NonNull LifecycleObserver observer) // Current state of the Lifecycle getCurrentState() // Compares the lifecycle states isAtLeast(@NonNull State state) ... } @nisrulz #DCBERLIN18

Slide 22

Slide 22 text

Lifecycle // Source code public abstract class Lifecycle { // Adds a LifecycleObserver addObserver(@NonNull LifecycleObserver observer) // Removes the given observer from the observers list removeObserver(@NonNull LifecycleObserver observer) // Current state of the Lifecycle getCurrentState() // Compares the lifecycle states isAtLeast(@NonNull State state) ... } @nisrulz #DCBERLIN18

Slide 23

Slide 23 text

LifecycleOwner @nisrulz #DCBERLIN18

Slide 24

Slide 24 text

LifecycleOwner Interface that goes through Android Lifecycle. @nisrulz #DCBERLIN18

Slide 25

Slide 25 text

LifecycleOwner Interface that goes through Android Lifecycle. // Source code public interface LifecycleOwner { // Returns the Lifecycle of the provider. @NonNull Lifecycle getLifecycle(); } @nisrulz #DCBERLIN18

Slide 26

Slide 26 text

LifecycleOwner class MainActivity : AppCompatActivity() { // Missing myLifecycleObserver override fun onResume() { // Add lifecycle observer lifecycle.addObserver(myLifecycleObserver) } override fun onStop() { // Remove lifecycle observer lifecycle.removeObserver(myLifecycleObserver) } } @nisrulz #DCBERLIN18

Slide 27

Slide 27 text

LifecycleOwner Fun Fact: Activities & fragments are not the only things with lifecycle by default @nisrulz #DCBERLIN18

Slide 28

Slide 28 text

LifecycleOwner Fun Fact: Activities & fragments are not the only things with lifecycle by default You also have - LifecycleService - ProcessLifecycleOwner @nisrulz #DCBERLIN18

Slide 29

Slide 29 text

LifecycleService @nisrulz #DCBERLIN18

Slide 30

Slide 30 text

LifecycleService A service that is also a LifecycleOwner @nisrulz #DCBERLIN18

Slide 31

Slide 31 text

LifecycleService A service that is also a LifecycleOwner // Source code public class LifecycleService extends Service implements LifecycleOwner { ... } @nisrulz #DCBERLIN18

Slide 32

Slide 32 text

ProcessLifecycleOwner @nisrulz #DCBERLIN18

Slide 33

Slide 33 text

ProcessLifecycleOwner - Class that tracks the lifecycle of @nisrulz #DCBERLIN18

Slide 34

Slide 34 text

ProcessLifecycleOwner - Class that tracks the lifecycle of - whole application process or - all the activities combined @nisrulz #DCBERLIN18

Slide 35

Slide 35 text

ProcessLifecycleOwner - Class that tracks the lifecycle of - whole application process or - all the activities combined - Useful for reacting to app coming to foreground or going to background @nisrulz #DCBERLIN18

Slide 36

Slide 36 text

ProcessLifecycleOwner dependencies { def lifecycle_version = "1.1.1" // For ProcessLifecycleOwner implementation "android.arch.lifecycle:extensions:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 37

Slide 37 text

ProcessLifecycleOwner ON_CREATE ON_START & ON_RESUME ON_PAUSE & ON_STOP ON_DESTROY @nisrulz #DCBERLIN18

Slide 38

Slide 38 text

ProcessLifecycleOwner ON_CREATE (Only 1 time) ON_START & ON_RESUME ON_PAUSE & ON_STOP ON_DESTROY @nisrulz #DCBERLIN18

Slide 39

Slide 39 text

ProcessLifecycleOwner ON_CREATE (Only 1 time) ON_START & ON_RESUME (First Activity pass) ON_PAUSE & ON_STOP ON_DESTROY @nisrulz #DCBERLIN18

Slide 40

Slide 40 text

ProcessLifecycleOwner ON_CREATE (Only 1 time) ON_START & ON_RESUME (First Activity pass) ON_PAUSE & ON_STOP (after 700ms delay) ON_DESTROY @nisrulz #DCBERLIN18

Slide 41

Slide 41 text

ProcessLifecycleOwner ON_CREATE (Only 1 time) ON_START & ON_RESUME (First Activity pass) ON_PAUSE & ON_STOP (after 700ms delay) ON_DESTROY @nisrulz #DCBERLIN18

Slide 42

Slide 42 text

ProcessLifecycleOwner ON_CREATE (Only 1 time) ON_START & ON_RESUME (First Activity pass) ON_PAUSE & ON_STOP (after 700ms delay) ON_DESTROY @nisrulz #DCBERLIN18

Slide 43

Slide 43 text

ProcessLifecycleOwner ON_PAUSE & ON_STOP (after 700ms delay) @nisrulz #DCBERLIN18

Slide 44

Slide 44 text

ProcessLifecycleOwner ON_PAUSE & ON_STOP (after 700ms delay) // Source code public class ProcessLifecycleOwner implements LifecycleOwner { static final long TIMEOUT_MS = 700; //mls ... } @nisrulz #DCBERLIN18

Slide 45

Slide 45 text

ProcessLifecycleOwner Guarantee that ProcessLifecycleOwner won’t send any events if activities are - Destroyed - Recreated due to configuration change @nisrulz #DCBERLIN18

Slide 46

Slide 46 text

ProcessLifecycleOwner Comes with cost: @nisrulz #DCBERLIN18

Slide 47

Slide 47 text

ProcessLifecycleOwner Comes with cost: extension artifact automatically adds element to your manifest @nisrulz #DCBERLIN18

Slide 48

Slide 48 text

ProcessLifecycleOwner ... @nisrulz #DCBERLIN18

Slide 49

Slide 49 text

ProcessLifecycleOwner // Source code // Internal class to initialize Lifecycles. public class ProcessLifecycleOwnerInitializer extends ContentProvider { @Override public boolean onCreate() { ... ProcessLifecycleOwner.init(getContext()); return true; } ... } @nisrulz #DCBERLIN18

Slide 50

Slide 50 text

ProcessLifecycleOwner Side-effect: @nisrulz #DCBERLIN18

Slide 51

Slide 51 text

ProcessLifecycleOwner Side-effect: Inits ProcessLifecycleOwner even if your app does not use it! @nisrulz #DCBERLIN18

Slide 52

Slide 52 text

ProcessLifecycleOwner Side-effect: Inits ProcessLifecycleOwner even if your app does not use it! Why? @nisrulz #DCBERLIN18

Slide 53

Slide 53 text

ProcessLifecycleOwner Side-effect: Inits ProcessLifecycleOwner even if your app does not use it! Why? So that ProcessLifecycleOwner can be invoked as soon as process starts @nisrulz #DCBERLIN18

Slide 54

Slide 54 text

@nisrulz #DCBERLIN18 Application.ActivityLifecycleCallback vs ProcessLifecycleOwner

Slide 55

Slide 55 text

@nisrulz #DCBERLIN18 Application.ActivityLifecycleCallback vs or ProcessLifecycleOwner

Slide 56

Slide 56 text

Application.ActivityLifecycleCallback // Source code public interface ActivityLifecycleCallbacks { void onActivityCreated(Activity var1, Bundle var2); ... void onActivitySaveInstanceState(Activity var1, Bundle var2); void onActivityDestroyed(Activity var1); } @nisrulz #DCBERLIN18

Slide 57

Slide 57 text

Application.ActivityLifecycleCallback // Register for the callback application .registerActivityLifecycleCallbacks(object: Application.ActivityLifecycleCallbacks{ override fun onActivityCreated(activity: Activity?, bundle: Bundle?) {} ... }) @nisrulz #DCBERLIN18

Slide 58

Slide 58 text

ProcessLifecycleOwner // Source code public class ProcessLifecycleOwner implements LifecycleOwner { ... void attach(Context context) { ... Application app = (Application) context.getApplicationContext(); app.registerActivityLifecycleCallbacks (new EmptyActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { .. } ... }); } } @nisrulz #DCBERLIN18

Slide 59

Slide 59 text

ProcessLifecycleOwner // Source code public class ProcessLifecycleOwner implements LifecycleOwner { ... void attach(Context context) { ... Application app = (Application) context.getApplicationContext(); app.registerActivityLifecycleCallbacks (new EmptyActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { .. } ... }); } } @nisrulz #DCBERLIN18

Slide 60

Slide 60 text

ProcessLifecycleOwner // Source code public class ProcessLifecycleOwner implements LifecycleOwner { ... void attach(Context context) { ... Application app = (Application) context.getApplicationContext(); app.registerActivityLifecycleCallbacks (new EmptyActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { .. } ... }); } } @nisrulz #DCBERLIN18

Slide 61

Slide 61 text

ProcessLifecycleOwner // Source code class EmptyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { //Empty method body } ... } @nisrulz #DCBERLIN18

Slide 62

Slide 62 text

ProcessLifecycleOwner // Source code class EmptyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { //Empty method body } ... } @nisrulz #DCBERLIN18

Slide 63

Slide 63 text

LifecycleRegistry @nisrulz #DCBERLIN18

Slide 64

Slide 64 text

LifecycleRegistry Implementation of Lifecycle that can handle multiple observers @nisrulz #DCBERLIN18

Slide 65

Slide 65 text

LifecycleRegistry Implementation of Lifecycle that can handle multiple observers // Source code public class LifecycleRegistry extends Lifecycle { ... } @nisrulz #DCBERLIN18

Slide 66

Slide 66 text

LifecycleRegistry Implementation of Lifecycle that can handle multiple observers // Source code public class LifecycleRegistry extends Lifecycle { ... } However, you need to dispatch the events yourself. @nisrulz #DCBERLIN18

Slide 67

Slide 67 text

LifecycleRegistry class MyLifecycleOwner : LifecycleOwner { private var registry: LifecycleRegistry = LifecycleRegistry(this) init { registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } override fun getLifecycle(): Lifecycle = registry fun startListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stopListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } } @nisrulz #DCBERLIN18

Slide 68

Slide 68 text

LifecycleRegistry class MyLifecycleOwner : LifecycleOwner { private var registry: LifecycleRegistry = LifecycleRegistry(this) init { registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } override fun getLifecycle(): Lifecycle = registry fun startListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stopListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } } @nisrulz #DCBERLIN18

Slide 69

Slide 69 text

LifecycleRegistry class MyLifecycleOwner : LifecycleOwner { private var registry: LifecycleRegistry = LifecycleRegistry(this) init { registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } override fun getLifecycle(): Lifecycle = registry fun startListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stopListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } } @nisrulz #DCBERLIN18

Slide 70

Slide 70 text

LifecycleRegistry class MyLifecycleOwner : LifecycleOwner { private var registry: LifecycleRegistry = LifecycleRegistry(this) init { registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } override fun getLifecycle(): Lifecycle = registry fun startListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stopListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } } @nisrulz #DCBERLIN18

Slide 71

Slide 71 text

LifecycleRegistry class MyLifecycleOwner : LifecycleOwner { private var registry: LifecycleRegistry = LifecycleRegistry(this) init { registry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) } override fun getLifecycle(): Lifecycle = registry fun startListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_START) } fun stopListening() { registry.handleLifecycleEvent(Lifecycle.Event.ON_STOP) } } @nisrulz #DCBERLIN18

Slide 72

Slide 72 text

LifecycleRegistry val myLifecycleOwner = MyLifecycleOwner() // Missing myLifecycleObserver // Add lifecycle observer myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver) // Remove lifecycle observer myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver) @nisrulz #DCBERLIN18

Slide 73

Slide 73 text

LifecycleObserver @nisrulz #DCBERLIN18

Slide 74

Slide 74 text

LifecycleObserver An interface for observing a Lifecycle // Source code public interface LifecycleObserver { // Empty } @nisrulz #DCBERLIN18

Slide 75

Slide 75 text

LifecycleObserver Two ways to implement - Annotation - Callback Interface @nisrulz #DCBERLIN18

Slide 76

Slide 76 text

LifecycleObserver dependencies { def lifecycle_version = "1.1.1" // For Annotation Support annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 77

Slide 77 text

LifecycleObserver class MyLifecycleObserver : LifecycleObserver { @OnLifecycleEvent(Lifecycle.Event.ON_CREATE) fun init() {} @OnLifecycleEvent(Lifecycle.Event.ON_START) fun onStart() {} ... @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) fun cleanup() {} } @nisrulz #DCBERLIN18

Slide 78

Slide 78 text

LifecycleObserver val myLifecycleOwner = MyLifecycleOwner() // Add lifecycle observer myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver) // Remove lifecycle observer myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver) @nisrulz #DCBERLIN18

Slide 79

Slide 79 text

LifecycleObserver val myLifecycleOwner = MyLifecycleOwner() val myLifecycleObserver = MyLifecycleObserver() // Add lifecycle observer myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver) // Remove lifecycle observer myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver) @nisrulz #DCBERLIN18

Slide 80

Slide 80 text

LifecycleObserver val myLifecycleOwner = MyLifecycleOwner() val myLifecycleObserver = MyLifecycleObserver() // Add lifecycle observer myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver) // Remove lifecycle observer myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver) @nisrulz #DCBERLIN18

Slide 81

Slide 81 text

DefaultLifecycleObserver @nisrulz #DCBERLIN18

Slide 82

Slide 82 text

DefaultLifecycleObserver Callback interface for listening to LifecycleOwner state changes. @nisrulz #DCBERLIN18

Slide 83

Slide 83 text

DefaultLifecycleObserver Callback interface for listening to LifecycleOwner state changes. Recommended over annotations if using Java 8 language. @nisrulz #DCBERLIN18

Slide 84

Slide 84 text

DefaultLifecycleObserver android { compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { def lifecycle_version = "1.1.1" // For DefaultLifecycleObserver implementation "android.arch.lifecycle:common-java8:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 85

Slide 85 text

DefaultLifecycleObserver // Source code public interface DefaultLifecycleObserver extends FullLifecycleObserver { @Override default void onCreate(@NonNull LifecycleOwner owner) { ... } ... @Override default void onDestroy(@NonNull LifecycleOwner owner) { ... } } @nisrulz #DCBERLIN18

Slide 86

Slide 86 text

DefaultLifecycleObserver // Source code interface FullLifecycleObserver extends LifecycleObserver { void onCreate(LifecycleOwner owner); void onStart(LifecycleOwner owner); ... void onDestroy(LifecycleOwner owner); } @nisrulz #DCBERLIN18

Slide 87

Slide 87 text

DefaultLifecycleObserver class MyLifecycleObserver : DefaultLifecycleObserver { override fun onCreate(owner: LifecycleOwner) {} ... override fun onDestroy(owner: LifecycleOwner) {} } @nisrulz #DCBERLIN18

Slide 88

Slide 88 text

LiveData @nisrulz #DCBERLIN18

Slide 89

Slide 89 text

LiveData A lifecycle aware base class for encapsulating loading data @nisrulz #DCBERLIN18

Slide 90

Slide 90 text

LiveData A lifecycle aware base class for encapsulating loading data Lightweight implementation of Observer pattern @nisrulz #DCBERLIN18

Slide 91

Slide 91 text

LiveData dependencies { def lifecycle_version = "1.1.1" // For LiveData implementation "android.arch.lifecycle:livedata:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 92

Slide 92 text

LiveData // Source Code public abstract class LiveData { ... protected void postValue(T value) {...} ... @MainThread protected void setValue(T value) {...} ... protected void onActive() {...} protected void onInactive() {...} } @nisrulz #DCBERLIN18

Slide 93

Slide 93 text

LiveData // Source Code public abstract class LiveData { ... protected void postValue(T value) {...} // Sets the value async ... @MainThread protected void setValue(T value) {...} ... protected void onActive() {...} protected void onInactive() {...} } @nisrulz #DCBERLIN18

Slide 94

Slide 94 text

LiveData // Source Code public abstract class LiveData { ... protected void postValue(T value) {...} // Sets the value async ... @MainThread protected void setValue(T value) {...} // Sets the value sync ... protected void onActive() {...} protected void onInactive() {...} } @nisrulz #DCBERLIN18

Slide 95

Slide 95 text

LiveData // Source Code public abstract class LiveData { ... protected void postValue(T value) {...} // Sets the value async ... @MainThread protected void setValue(T value) {...} // Sets the value sync ... protected void onActive() {...} // Has active observers i.e in Start/Resume state protected void onInactive() {...} } @nisrulz #DCBERLIN18

Slide 96

Slide 96 text

LiveData // Source Code public abstract class LiveData { ... protected void postValue(T value) {...} // Sets the value async ... @MainThread protected void setValue(T value) {...} // Sets the value sync ... protected void onActive() {...} // Has active observers i.e in Start/Resume state protected void onInactive() {...} // Has 0 active observers } @nisrulz #DCBERLIN18

Slide 97

Slide 97 text

LiveData class MySensorLiveData(context: Context?) : LiveData() { val sensorManager: SensorManager? = context? .getSystemService(Service.SENSOR_SERVICE) as SensorManager val sensorListener: SensorEventListener = object : SensorEventListener { override fun onSensorChanged(event: SensorEvent?) { // Set Value setValue(event?.values?.get(0)) } ... } } @nisrulz #DCBERLIN18

Slide 98

Slide 98 text

LiveData class MySensorLiveData(context: Context?) : LiveData() { val sensorManager: SensorManager? = context? .getSystemService(Service.SENSOR_SERVICE) as SensorManager val sensorListener: SensorEventListener = object : SensorEventListener { override fun onSensorChanged(event: SensorEvent?) { // Set Value setValue(event?.values?.get(0)) } ... } } @nisrulz #DCBERLIN18

Slide 99

Slide 99 text

LiveData class MySensorLiveData(context: Context?) : LiveData() { ... override fun onActive() { ... sensorManager?.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST) } override fun onInactive() { ... sensorManager.unregisterListener(sensorListener) } } @nisrulz #DCBERLIN18

Slide 100

Slide 100 text

LiveData class MySensorLiveData(context: Context?) : LiveData() { ... override fun onActive() { ... sensorManager?.registerListener(sensorListener, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_FASTEST) } override fun onInactive() { ... sensorManager.unregisterListener(sensorListener) } } @nisrulz #DCBERLIN18

Slide 101

Slide 101 text

LiveData val mySensorLiveData = MySensorLiveData(this) // Observe for values mySensorLiveData.observe(this, Observer{ floatVal -> // Update the UI }) @nisrulz #DCBERLIN18

Slide 102

Slide 102 text

ViewModel @nisrulz #DCBERLIN18

Slide 103

Slide 103 text

ViewModel - Hold & manage UI related data in Lifecycle conscious way - Survives configuration changes - Prevents unnecessary reloading of data @nisrulz #DCBERLIN18

Slide 104

Slide 104 text

ViewModel dependencies { def lifecycle_version = "1.1.1" // For ViewModel implementation "android.arch.lifecycle:viewmodel:$lifecycle_version" ... } @nisrulz #DCBERLIN18

Slide 105

Slide 105 text

ViewModel // Source code public abstract class ViewModel { // Called when ViewModel is destroyed protected void onCleared() { } } @nisrulz #DCBERLIN18

Slide 106

Slide 106 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 107

Slide 107 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 108

Slide 108 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 109

Slide 109 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 110

Slide 110 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 111

Slide 111 text

ViewModel class MyViewModel : ViewModel() { private val username = MutableLiveData() fun initExpensiveOperation() { // expensive operation, e.g. network request username.value = "Nishant" // username.setValue(“Nishant”) } fun getUsername(): LiveData { return username } } @nisrulz #DCBERLIN18

Slide 112

Slide 112 text

ViewModel val viewModel = ViewModelProviders .of(this) .get(MyViewModel::class.java) viewModel.getUsername().observe(this, Observer { text -> //Update the UI }) @nisrulz #DCBERLIN18

Slide 113

Slide 113 text

ViewModel val viewModel = ViewModelProviders .of(this) .get(MyViewModel::class.java) viewModel.getUsername().observe(this, Observer { text -> //Update the UI }) @nisrulz #DCBERLIN18

Slide 114

Slide 114 text

Thank You twitter.com/nisrulz github.com/nisrulz www.nisrulz.com

Slide 115

Slide 115 text

More Info Is your Android Library, Lifecycle-Aware? https://android.jlelse.eu/is-your-android-library-lifecycle-aware-127629d32dcc Official documentation about lifecycle components d.android.com/topic/libraries/architecture/lifecycle Using ProcessLifecycleOwner for libraries example https://github.com/nisrulz/android-examples/tree/develop/UsingProcessLifecycleOwnerForLibs Using Lifecycle Components for libraries example https://github.com/nisrulz/android-examples/tree/develop/LifeCycleCompForLib @nisrulz #DCBERLIN18

Slide 116

Slide 116 text

The A,B and C of Lifecycle Components twitter.com/nisrulz github.com/nisrulz www.nisrulz.com