Android Fragments, and Live data Flashcards
Build understanding of android fragments, Live data and View Model and the Recycler view
What is a fragment (2)
- they are reusable components of a UI and an be used in different activities
- They are modular components of an activity
What are two key things that fragments enable you to do
1.Separate the navigation components from the content
2. Modifythe appearance of an activity at runtime
What library support fragments
Androidx.fragment
What are the key lifecycle stages for a fragment
{onAttach}
onCreate(Context context)
onCreateView(Bundle, savedInstanceState)
onViewCreated(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) - create the fragment’s view hierarchy. It is similar to onCreate, but it is responsible only for the view inflation
onViewStateRestored
onStart
onResume
onPause
onStop
onSavedInstanceState
onDestroyView
onDestroy
{onDetach}
What are the advantages (2) and disadvantages of fragments (1)
- code reuse and modularity
- ability to build multi-pane interfaces (useful for tablets)
- Disadvantage is the increased code complexity
Do fragments need to include a container,if so what is the recommended one?
Yes - this is in the layout file.
yes, it makes management easier. Most common is the FragmentContainerView
How would you set up the code to use fragments
- Create a fragments layout file and class
- Add a fragments container to the the activities layout (FragmentContainerView)
- Get a FragmentManager instance using getSupportFragmentManager()
- Create a fragments instance
- Commit the transaction
- Communicate between fragments
How are fragments loaded
using the FragmentManager and FragmentTransaction
What is the Fragment manager responsible for
Attaching fragments to the host activity and detaching them when they are no longer in user
How is data passed between fragments
Using LiveData
What is LiveData and what does it allow
Architecture component in Android JetPack that implements the Observer pattern.
- permits observers to subscribe to updates for data held by the LiveData object and then when it changes they are notified
- it only updates observers that are in the active lifecycle state
How can LiveData be implemented
A fragment or activity class can implement the Observer interface and be informed of changes in the observable objects
What is ViewModel, what is it’s job, is it destroyed?
Architecture component of jetpack
It manages and prepares data for a fragment or an activity
It is not destroyed, it retains the data when an activity or fragment is destroyed
How do LiveData and ViewModel fit together
LiveData is created as an instance within the View model class.
What is the difference between LiveData and MutableLiveData (4 key points)
LiveData is an abstract class and can not be instantiated
MutableLiveData extends Livedata (and is not abstract)
MutableLiveData has two methods to modify its data setValue and postValue
The ViewModel only exposes the immutable LiveData Objects to the observers