Android Fragments, and Live data Flashcards

Build understanding of android fragments, Live data and View Model and the Recycler view

1
Q

What is a fragment (2)

A
  • they are reusable components of a UI and an be used in different activities
  • They are modular components of an activity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are two key things that fragments enable you to do

A

1.Separate the navigation components from the content
2. Modifythe appearance of an activity at runtime

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

What library support fragments

A

Androidx.fragment

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

What are the key lifecycle stages for a fragment

A

{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}

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

What are the advantages (2) and disadvantages of fragments (1)

A
  • code reuse and modularity
  • ability to build multi-pane interfaces (useful for tablets)
  • Disadvantage is the increased code complexity
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Do fragments need to include a container,if so what is the recommended one?

A

Yes - this is in the layout file.
yes, it makes management easier. Most common is the FragmentContainerView

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

How would you set up the code to use fragments

A
  1. Create a fragments layout file and class
  2. Add a fragments container to the the activities layout (FragmentContainerView)
  3. Get a FragmentManager instance using getSupportFragmentManager()
  4. Create a fragments instance
  5. Commit the transaction
  6. Communicate between fragments
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are fragments loaded

A

using the FragmentManager and FragmentTransaction

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

What is the Fragment manager responsible for

A

Attaching fragments to the host activity and detaching them when they are no longer in user

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

How is data passed between fragments

A

Using LiveData

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

What is LiveData and what does it allow

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can LiveData be implemented

A

A fragment or activity class can implement the Observer interface and be informed of changes in the observable objects

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

What is ViewModel, what is it’s job, is it destroyed?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do LiveData and ViewModel fit together

A

LiveData is created as an instance within the View model class.

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

What is the difference between LiveData and MutableLiveData (4 key points)

A

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

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

What are the steps to using LiveData

A
  1. Add implementation to build.gradle for androidx lifecycle viewModel and livedata
  2. Create ViewModel class to provide access to LiveData
  3. Create an instance of MutableLiveData so we can update the data by calling the setValue method of MutableLiveData
  4. Add the getText() to get the LiveData
  5. Use ViewModelProvider to get an instance of ViewModel to access the LiveData
  6. Make the main activity an observer and attach the observer object to the LiveData object
  7. Use ViewModel Provider to get an instance of ViewModel to access the LiveData
  8. Update the LiveData
17
Q

What is the Recycler View and when is it recommended

A

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
18
Q

What does the RecyclerView Adapter do

A

Provides methods to create ViewHolder objects as needed and bind them with data

19
Q

What are the steps to use the Recycler view

A
  1. add the Recycler View library to build.gradle
  2. In the activity_main.xml add a RecyclerView Widget
  3. Create a custom XML file with the views to be displayed in each of the rows of the Recycler view
  4. define a model class that deals with the data
  5. Define an adapter subclass that extends the RecyclerView.Adapter
  6. Implement a nested ViewHolder class
  7. Override methods in the RecyclerView.Adapter
  8. Use the onCreateViewHolder method to create and Initialise the ViewHolder
  9. Implement the onBindViewHolder
  10. Add getItemCount to return the size of your dataset
  11. instantiate the Recycler view in the activity and set an adaptor for it
  12. Create a layout manager and apply it to the recyclerView
  13. To reflect the updates to the data in the RecyclerView call the notifyDataSetChanged() method
20
Q

Do you need to create a FragmentManager?

A

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.