Lifecycle Flashcards
Reproduce the diagrams for:
- Activity lifecycle
- Fragment lifecycle
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
What must all overridden lifecycle functions do immediately?
Call super
What must you remember to do if you set up a resource in a lifecycle method?
Tear it down in its corresponding lifecycle method.
eg resources set up in onStart( ) should be torn down in onStop( )
What er the three main parts of the Lifecycle library?
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
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?
- Have the resource’s class extend Lifecycle Observer
- Pass a lifecycle into the resource’s class constructor
- Add init { lifecycle.addObserver(this) } to the resource’s class
- Annotate resource’s relevant class functions with @OnLifecycleEvent(Lifecycle.Event.LIFECYCLE_METHOD)
- When initialising the resource in Fragment / Activity, pass in (this.lifecycle)
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?
onSaveInstanceState( )
onCreate( )
onRestoreInstanceState( ) // useful if required when app has not just started
How to add / extract values from a Bundle?
- 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) }
- Following null check, GET k-v pairs from savedInstanceState (which is a Bundle) in onCreate / onRestoreInstanceState:
myVal = savedInstanceState.getInt(KEY_CONST, defaultValue)
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?
- 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