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
What are the steps to using LiveData
- Add implementation to build.gradle for androidx lifecycle viewModel and livedata
- Create ViewModel class to provide access to LiveData
- Create an instance of MutableLiveData so we can update the data by calling the setValue method of MutableLiveData
- Add the getText() to get the LiveData
- Use ViewModelProvider to get an instance of ViewModel to access the LiveData
- Make the main activity an observer and attach the observer object to the LiveData object
- Use ViewModel Provider to get an instance of ViewModel to access the LiveData
- Update the LiveData
What is the Recycler View and when is it recommended
ViewGroup
used for displaying lists of data
recycles it’s view for displaying new items
- Recommended when the size of the data is large and there is scrolling
- & when the data frequently changes
What does the RecyclerView Adapter do
Provides methods to create ViewHolder objects as needed and bind them with data
What are the steps to use the Recycler view
- add the Recycler View library to build.gradle
- In the activity_main.xml add a RecyclerView Widget
- Create a custom XML file with the views to be displayed in each of the rows of the Recycler view
- define a model class that deals with the data
- Define an adapter subclass that extends the RecyclerView.Adapter
- Implement a nested ViewHolder class
- Override methods in the RecyclerView.Adapter
- Use the onCreateViewHolder method to create and Initialise the ViewHolder
- Implement the onBindViewHolder
- Add getItemCount to return the size of your dataset
- instantiate the Recycler view in the activity and set an adaptor for it
- Create a layout manager and apply it to the recyclerView
- To reflect the updates to the data in the RecyclerView call the notifyDataSetChanged() method
Do you need to create a FragmentManager?
No, the FragmentManager is not a standalone object, but rather tied to the activity or fragment’s lifecycle.
It’s automatically created and destroyed along with the activity or fragment.
You don’t need to worry about creating it yourself, just access it using the provided methods.