Reproduce the diagrams for:
Activity --------------------------- RESUMED (focus, visible) onResume / onPause STARTED (visible) onRestoreInstanceState / onStart / onStop / **onSaveInstanceState *onRestart / CREATED onCreate / onDestroy INITIALISED / DESTROYED
**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
Lifecycle CLASS
- Holds the actual state of a lifecycle owner and triggers events when lifecycle changes happen
Lifecycle OBSERVERS
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?
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?
2. PUT k-v pairs into the bundle
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt(KEY_CONST, value)
}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?
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