Lifecycle Flashcards

1
Q

Reproduce the diagrams for:

  • Activity lifecycle
  • Fragment lifecycle
A
Activity
---------------------------
RESUMED (focus, visible)
onResume / onPause
STARTED (visible)
onRestoreInstanceState / 
onStart / onStop
 / **onSaveInstanceState
*onRestart / 
CREATED
onCreate / onDestroy
INITIALISED / DESTROYED
  • onRestart is called instead of onCreate when app is not starting for the first time

**onSaveInstanceState - a safety measure used to save a small amount of information to a bundle as the activity exits the foreground (in case app is killed by OS later - it will not call onDestroy( ))

Fragment
---------------------------
RESUMED (focus, visible)
onResume / onPause
STARTED (visible)
onStart / onStop
onViewCreated / onDestroyView
onCreateView / 
CREATED
onCreate / onDestroy
onAttach / onDetach
INITIALISED / DESTROYED
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What must all overridden lifecycle functions do immediately?

A

Call super

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What must you remember to do if you set up a resource in a lifecycle method?

A

Tear it down in its corresponding lifecycle method.

eg resources set up in onStart( ) should be torn down in onStop( )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What er the three main parts of the Lifecycle library?

A

Lifecycle OWNERS

  • Activity and Fragment are lifecycle owners
  • They implement the LifecycleOwner interface

Lifecycle CLASS
- Holds the actual state of a lifecycle owner and triggers events when lifecycle changes happen

Lifecycle OBSERVERS

  • Observe the lifecycle state and perform tasks when the lifecycle changes
  • They implement the LifecycleObserver interface
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Instead of setting up and tearing down a resource object in the lifecycle methods of an Activity / Fragment, how can you modify the resource itself so it can be managed by the Lifecycle library?

A
  1. Have the resource’s class extend Lifecycle Observer
  2. Pass a lifecycle into the resource’s class constructor
  3. Add init { lifecycle.addObserver(this) } to the resource’s class
  4. Annotate resource’s relevant class functions with @OnLifecycleEvent(Lifecycle.Event.LIFECYCLE_METHOD)
  5. When initialising the resource in Fragment / Activity, pass in (this.lifecycle)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which lifecycle method is used to save a bundle before the app is killed?

which lifecycle methods take this bundle as a parameter for handling?

A

onSaveInstanceState( )

onCreate( )
onRestoreInstanceState( ) // useful if required when app has not just started

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How to add / extract values from a Bundle?

A
  1. Define String constants for bundle keys
2. PUT k-v pairs into the bundle
override fun onSaveInstanceState(outState: Bundle) {
  super.onSaveInstanceState(outState)
  outState.putInt(KEY_CONST, value)
}
  1. Following null check, GET k-v pairs from savedInstanceState (which is a Bundle) in onCreate / onRestoreInstanceState:
    myVal = savedInstanceState.getInt(KEY_CONST, defaultValue)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What might trigger a configuration change (causing OS to shut down and re build the Activity)?

How do we avoid having to handle configuration changes manually?

A
  • user changes language
  • plugging device into a dock
  • user adds a physical keyboard
  • orientation change

onDestroy( ) and onCreate( ) will be called in these instances

ViewModels survive configuration changes. Using Architecture Components (ViewModels, ViewModelProviders, LiveData etc) obviates the need to use onSaveInstanceState( ) etc

How well did you know this?
1
Not at all
2
3
4
5
Perfectly