1 of 116

The A,B and C

of

Lifecycle Components

Nishant Srivastava

2 of 116

Nishant Srivastava

twitter.com/nisrulz

github.com/nisrulz

www.nisrulz.com

3 of 116

Soundbrenner

4 of 116

The Challenge

@nisrulz #DCBERLIN18

5 of 116

The Challenge

Android activity lifecycle..

@nisrulz #DCBERLIN18

6 of 116

The Challenge

Android activity lifecycle

@nisrulz #DCBERLIN18

7 of 116

The Challenge

Android activity lifecycle

  • Many states

​

@nisrulz #DCBERLIN18

8 of 116

The Challenge

Android activity lifecycle

  • Many states
  • Repeats on configuration changes

​

@nisrulz #DCBERLIN18

9 of 116

The Challenge

Android activity lifecycle

  • Many states
  • Repeats on configuration changes

​

​

Not to forget...

@nisrulz #DCBERLIN18

10 of 116

The Challenge

...the Fragment lifecycle.

@nisrulz #DCBERLIN18

11 of 116

The Challenge

...the Fragment lifecycle.

@nisrulz #DCBERLIN18

12 of 116

Architecture

Components

@nisrulz #DCBERLIN18

13 of 116

Lifecycle

Components

@nisrulz #DCBERLIN18

14 of 116

Lifecycle Components

Series of classes designed to help deal with lifecycles in a more consistent fashion.

​

@nisrulz #DCBERLIN18

15 of 116

Lifecycle Components

Series of classes designed to help deal with lifecycles in a more consistent fashion.

​

  • Lifecycle
  • LifecycleOwner
  • LifecycleObserver

@nisrulz #DCBERLIN18

16 of 116

Lifecycle

@nisrulz #DCBERLIN18

17 of 116

Lifecycle

Series of states an object can be in.

​

For Android:�Object that defines Android lifecycle states. i.e. Activity & Fragment

@nisrulz #DCBERLIN18

18 of 116

Lifecycle

@nisrulz #DCBERLIN18

19 of 116

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

20 of 116

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

21 of 116

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

22 of 116

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

23 of 116

LifecycleOwner

@nisrulz #DCBERLIN18

24 of 116

LifecycleOwner

Interface that goes through Android Lifecycle.

@nisrulz #DCBERLIN18

25 of 116

LifecycleOwner

Interface that goes through Android Lifecycle.

​

// Source code

public interface LifecycleOwner {� // Returns the Lifecycle of the provider.� @NonNull� Lifecycle getLifecycle();�}

@nisrulz #DCBERLIN18

26 of 116

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

27 of 116

LifecycleOwner

Fun Fact:�Activities & fragments are not the only things with lifecycle by default

@nisrulz #DCBERLIN18

28 of 116

LifecycleOwner

Fun Fact:�Activities & fragments are not the only things with lifecycle by default

​

You also have

  • LifecycleService
  • ProcessLifecycleOwner

@nisrulz #DCBERLIN18

29 of 116

LifecycleService

@nisrulz #DCBERLIN18

30 of 116

LifecycleService

A service that is also a LifecycleOwner

@nisrulz #DCBERLIN18

31 of 116

LifecycleService

A service that is also a LifecycleOwner

​

// Source code

public class LifecycleService extends Service implements LifecycleOwner {� ...�}

@nisrulz #DCBERLIN18

32 of 116

ProcessLifecycleOwner

@nisrulz #DCBERLIN18

33 of 116

ProcessLifecycleOwner

  • Class that tracks the lifecycle of

@nisrulz #DCBERLIN18

34 of 116

ProcessLifecycleOwner

  • Class that tracks the lifecycle of
  • whole application process

or

    • all the activities combined

@nisrulz #DCBERLIN18

35 of 116

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

36 of 116

ProcessLifecycleOwner

dependencies {� def lifecycle_version = "1.1.1"

� // For ProcessLifecycleOwner� implementation "android.arch.lifecycle:extensions:$lifecycle_version"� ...�}

@nisrulz #DCBERLIN18

37 of 116

ProcessLifecycleOwner

ON_CREATE

ON_START & ON_RESUME

ON_PAUSE & ON_STOP

ON_DESTROY

@nisrulz #DCBERLIN18

38 of 116

ProcessLifecycleOwner

ON_CREATE (Only 1 time)

ON_START & ON_RESUME

ON_PAUSE & ON_STOP

ON_DESTROY

@nisrulz #DCBERLIN18

39 of 116

ProcessLifecycleOwner

ON_CREATE (Only 1 time)

ON_START & ON_RESUME (First Activity pass)

ON_PAUSE & ON_STOP

ON_DESTROY

@nisrulz #DCBERLIN18

40 of 116

ProcessLifecycleOwner

ON_CREATE (Only 1 time)

ON_START & ON_RESUME (First Activity pass)

ON_PAUSE & ON_STOP (after 700ms delay)

ON_DESTROY

@nisrulz #DCBERLIN18

41 of 116

ProcessLifecycleOwner

ON_CREATE (Only 1 time)

ON_START & ON_RESUME (First Activity pass)

ON_PAUSE & ON_STOP (after 700ms delay)

ON_DESTROY

@nisrulz #DCBERLIN18

42 of 116

ProcessLifecycleOwner

ON_CREATE (Only 1 time)

ON_START & ON_RESUME (First Activity pass)

ON_PAUSE & ON_STOP (after 700ms delay)

ON_DESTROY

@nisrulz #DCBERLIN18

43 of 116

ProcessLifecycleOwner

ON_PAUSE & ON_STOP (after 700ms delay)

@nisrulz #DCBERLIN18

44 of 116

ProcessLifecycleOwner

ON_PAUSE & ON_STOP (after 700ms delay)

​

// Source code

public class ProcessLifecycleOwner implements LifecycleOwner {� static final long TIMEOUT_MS = 700; //mls� ...�}

@nisrulz #DCBERLIN18

45 of 116

ProcessLifecycleOwner

Guarantee that ProcessLifecycleOwner won’t send any events if activities are

  • Destroyed
  • Recreated due to configuration change

@nisrulz #DCBERLIN18

46 of 116

ProcessLifecycleOwner

Comes with cost:

@nisrulz #DCBERLIN18

47 of 116

ProcessLifecycleOwner

Comes with cost:

​

extension artifact automatically adds <provider> element to your manifest

​

@nisrulz #DCBERLIN18

48 of 116

ProcessLifecycleOwner

<manifest >� <application>� ...� <provider

android:name="android.arch.lifecycle.ProcessLifecycleOwnerInitializer"

android:authorities="com.example.app.lifecycle-trojan"� android:exported="false"� android:multiprocess="true" />

</application>

</manifest>

@nisrulz #DCBERLIN18

49 of 116

ProcessLifecycleOwner

// Source code

// Internal class to initialize Lifecycles.�public class ProcessLifecycleOwnerInitializer extends ContentProvider {� @Override� public boolean onCreate() {� ...

ProcessLifecycleOwner.init(getContext());� return true;� }� ...�}

​

@nisrulz #DCBERLIN18

50 of 116

ProcessLifecycleOwner

Side-effect:

@nisrulz #DCBERLIN18

51 of 116

ProcessLifecycleOwner

Side-effect:

Inits ProcessLifecycleOwner even if your app does not use it!

@nisrulz #DCBERLIN18

52 of 116

ProcessLifecycleOwner

Side-effect:

Inits ProcessLifecycleOwner even if your app does not use it!�

Why?

@nisrulz #DCBERLIN18

53 of 116

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

54 of 116

@nisrulz #DCBERLIN18

Application.ActivityLifecycleCallback

vs

ProcessLifecycleOwner

55 of 116

@nisrulz #DCBERLIN18

Application.ActivityLifecycleCallback

vs or

ProcessLifecycleOwner

56 of 116

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

57 of 116

Application.ActivityLifecycleCallback

// Register for the callback

application� .registerActivityLifecycleCallbacks(object:� Application.ActivityLifecycleCallbacks{� override fun onActivityCreated(activity: Activity?, bundle: Bundle?) {}� ...� })

​

@nisrulz #DCBERLIN18

58 of 116

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

59 of 116

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

60 of 116

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

61 of 116

ProcessLifecycleOwner

// Source code

class EmptyActivityLifecycleCallbacks� implements Application.ActivityLifecycleCallbacks {�� @Override� public void onActivityCreated(Activity activity,� Bundle savedInstanceState) {

//Empty method body� }�� ...�}

​

@nisrulz #DCBERLIN18

62 of 116

ProcessLifecycleOwner

// Source code

class EmptyActivityLifecycleCallbacks� implements Application.ActivityLifecycleCallbacks {�� @Override� public void onActivityCreated(Activity activity,� Bundle savedInstanceState) {

//Empty method body� }�� ...�}

​

@nisrulz #DCBERLIN18

63 of 116

LifecycleRegistry

@nisrulz #DCBERLIN18

64 of 116

LifecycleRegistry

Implementation of Lifecycle that can handle multiple observers

@nisrulz #DCBERLIN18

65 of 116

LifecycleRegistry

Implementation of Lifecycle that can handle multiple observers

​

// Source code

public class LifecycleRegistry extends Lifecycle {� ...�}

@nisrulz #DCBERLIN18

66 of 116

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

67 of 116

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

68 of 116

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

69 of 116

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

70 of 116

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

71 of 116

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

72 of 116

LifecycleRegistry

val myLifecycleOwner = MyLifecycleOwner()��// Missing myLifecycleObserver��// Add lifecycle observer�myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver)

​

// Remove lifecycle observer�myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver)

​

@nisrulz #DCBERLIN18

73 of 116

LifecycleObserver

@nisrulz #DCBERLIN18

74 of 116

LifecycleObserver

An interface for observing a Lifecycle

​

// Source code�public interface LifecycleObserver {

// Empty

}

@nisrulz #DCBERLIN18

75 of 116

LifecycleObserver

Two ways to implement

​

  • Annotation
  • Callback Interface

@nisrulz #DCBERLIN18

76 of 116

LifecycleObserver

dependencies {� def lifecycle_version = "1.1.1"

� // For Annotation Support

annotationProcessor "android.arch.lifecycle:compiler:$lifecycle_version"� ...�}

@nisrulz #DCBERLIN18

77 of 116

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

78 of 116

LifecycleObserver

val myLifecycleOwner = MyLifecycleOwner()����// Add lifecycle observer�myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver)

​

// Remove lifecycle observer�myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver)

​

@nisrulz #DCBERLIN18

79 of 116

LifecycleObserver

val myLifecycleOwner = MyLifecycleOwner()��val myLifecycleObserver = MyLifecycleObserver()��// Add lifecycle observer�myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver)

​

// Remove lifecycle observer�myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver)

​

@nisrulz #DCBERLIN18

80 of 116

LifecycleObserver

val myLifecycleOwner = MyLifecycleOwner()��val myLifecycleObserver = MyLifecycleObserver()��// Add lifecycle observer�myLifecycleOwner.lifecycle.addObserver(myLifecycleObserver)

​

// Remove lifecycle observer�myLifecycleOwner.lifecycle.removeObserver(myLifecycleObserver)

​

@nisrulz #DCBERLIN18

81 of 116

DefaultLifecycleObserver

@nisrulz #DCBERLIN18

82 of 116

DefaultLifecycleObserver

Callback interface for listening to LifecycleOwner state changes.

​

@nisrulz #DCBERLIN18

83 of 116

DefaultLifecycleObserver

Callback interface for listening to LifecycleOwner state changes.

​

Recommended over annotations if using Java 8 language.

@nisrulz #DCBERLIN18

84 of 116

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

85 of 116

DefaultLifecycleObserver

// Source code

public interface DefaultLifecycleObserver extends FullLifecycleObserver {

� @Override� default void onCreate(@NonNull LifecycleOwner owner) { ... }�� ...�� @Override� default void onDestroy(@NonNull LifecycleOwner owner) { ... }�}

@nisrulz #DCBERLIN18

86 of 116

DefaultLifecycleObserver

// Source code

interface FullLifecycleObserver extends LifecycleObserver {

� void onCreate(LifecycleOwner owner);

� void onStart(LifecycleOwner owner);�

...

� void onDestroy(LifecycleOwner owner);�}

@nisrulz #DCBERLIN18

87 of 116

DefaultLifecycleObserver

class MyLifecycleObserver : DefaultLifecycleObserver {�� override fun onCreate(owner: LifecycleOwner) {}� ...� override fun onDestroy(owner: LifecycleOwner) {}�}

@nisrulz #DCBERLIN18

88 of 116

LiveData

@nisrulz #DCBERLIN18

89 of 116

LiveData

A lifecycle aware base class for encapsulating loading data

@nisrulz #DCBERLIN18

90 of 116

LiveData

A lifecycle aware base class for encapsulating loading data

​

Lightweight implementation of Observer pattern

​

@nisrulz #DCBERLIN18

91 of 116

LiveData

dependencies {� def lifecycle_version = "1.1.1"� // For LiveData� implementation "android.arch.lifecycle:livedata:$lifecycle_version"� ...�}

​

@nisrulz #DCBERLIN18

92 of 116

LiveData

// Source Code�public abstract class LiveData<T> {� ...� protected void postValue(T value) {...} � ...� @MainThread� protected void setValue(T value) {...} � ...� protected void onActive() {...} � protected void onInactive() {...} �}

@nisrulz #DCBERLIN18

93 of 116

LiveData

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

@nisrulz #DCBERLIN18

94 of 116

LiveData

// Source Code�public abstract class LiveData<T> {� ...� 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

95 of 116

LiveData

// Source Code�public abstract class LiveData<T> {� ...� 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

96 of 116

LiveData

// Source Code�public abstract class LiveData<T> {� ...� 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

97 of 116

LiveData

class MySensorLiveData(context: Context?) : LiveData<Float>() {� 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

98 of 116

LiveData

class MySensorLiveData(context: Context?) : LiveData<Float>() {� 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

99 of 116

LiveData

class MySensorLiveData(context: Context?) : LiveData<Float>() {� ...� override fun onActive() {� ...� sensorManager?.registerListener(sensorListener,� sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),� SensorManager.SENSOR_DELAY_FASTEST)� }�� override fun onInactive() {� ...� sensorManager.unregisterListener(sensorListener)� }�}

​

@nisrulz #DCBERLIN18

100 of 116

LiveData

class MySensorLiveData(context: Context?) : LiveData<Float>() {� ...� override fun onActive() {� ...� sensorManager?.registerListener(sensorListener,� sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),� SensorManager.SENSOR_DELAY_FASTEST)� }�� override fun onInactive() {� ...� sensorManager.unregisterListener(sensorListener)� }�}

​

@nisrulz #DCBERLIN18

101 of 116

LiveData

val mySensorLiveData = MySensorLiveData(this)

�// Observe for values�mySensorLiveData.observe(this, Observer<Float>{ floatVal ->� // Update the UI�})

@nisrulz #DCBERLIN18

102 of 116

ViewModel

@nisrulz #DCBERLIN18

103 of 116

ViewModel

  • Hold & manage UI related data in Lifecycle conscious way

​

  • Survives configuration changes

​

  • Prevents unnecessary reloading of data

@nisrulz #DCBERLIN18

104 of 116

ViewModel

dependencies {� def lifecycle_version = "1.1.1"� // For ViewModel� implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"� ...�}

@nisrulz #DCBERLIN18

105 of 116

ViewModel

// Source code�public abstract class ViewModel {

​

// Called when ViewModel is destroyed� protected void onCleared() {� }�}

​

@nisrulz #DCBERLIN18

106 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

107 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

108 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

109 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

110 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

111 of 116

ViewModel

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

​

@nisrulz #DCBERLIN18

112 of 116

ViewModel

val viewModel = ViewModelProviders� .of(this)� .get(MyViewModel::class.java)��viewModel.getUsername().observe(this, Observer<String> { text ->� //Update the UI�})

@nisrulz #DCBERLIN18

113 of 116

ViewModel

val viewModel = ViewModelProviders� .of(this)� .get(MyViewModel::class.java)��viewModel.getUsername().observe(this, Observer<String> { text ->� //Update the UI�})

@nisrulz #DCBERLIN18

114 of 116

Thank You

twitter.com/nisrulz

github.com/nisrulz

www.nisrulz.com

115 of 116

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

116 of 116

The A,B and C

of

Lifecycle Components

twitter.com/nisrulz

github.com/nisrulz

www.nisrulz.com