1/34
30 practice flashcards covering fragment lifecycle, static and dynamic fragments, dual-pane layouts, and Jetpack Navigation concepts from the lecture notes.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a Fragment in Android?
A modular UI component that represents a portion of an Activity's UI and can be reused across multiple activities.
How is the fragment lifecycle related to the Activity lifecycle?
The fragment lifecycle is bound to the Activity lifecycle; fragment callbacks are interleaved with the Activity's lifecycle.
Name five common Fragment lifecycle callbacks.
onAttach, onCreate, onCreateView, onViewCreated, onStart (with onResume as the next step).
Which callback creates the fragment's UI?
onCreateView.
Which callback is typically used to set up the fragment's views after the layout is created?
onViewCreated.
What is the difference between static and dynamic fragments?
Static fragments are defined in the activity's layout XML; dynamic fragments are added or replaced at runtime via FragmentManager.
What is a dual-pane layout?
A layout that shows two panes on larger screens to display, for example, a list and a detail simultaneously.
What does the sw600dp qualifier indicate?
A resource qualifier indicating devices with a smallest width greater than 600 dp (usually 7-inch tablets and larger).
What is Jetpack Navigation used for?
To simplify navigation between fragments and activities using a graph of destinations and actions.
What is a navigation graph?
An XML resource defining destinations (fragments/activities) and actions that connect them.
How do you add a static fragment to an activity layout?
By including a fragment tag in the layout with android:name set to the fragment class.
How can you create a two-fragment layout with proportional space?
Place two fragment elements in a vertical LinearLayout with layoutheight 0dp and assign layoutweight values to divide space.
How do you pass data to a fragment using a Bundle?
Create a Bundle with key-value pairs and set it as the fragment's arguments before adding it.
What is a fragment factory method and how is it used?
A companion object newInstance(param) that returns a Fragment with its arguments set, enabling parameterized fragments.
What is FragmentContainerView?
A container view that hosts fragments and works well with Jetpack Navigation.
In a dual-pane layout, how does an activity update the detail pane when a list item is selected?
It checks isDualPane; if true, it updates the detail fragment; otherwise, it starts a separate DetailActivity.
What does addToBackStack(null) do in fragment transactions?
Adds the transaction to the back stack so the user can press back to return to the previous fragment.
How can fragments communicate with their hosting activity?
Define a listener interface in the fragment; the activity implements it and the fragment calls the callback.
What steps are involved in using Jetpack Navigation in an app?
Add a NavHostFragment, create a nav_graph.xml with destinations and actions, and wire the host to use the graph.
How can you navigate between destinations using a fragment's view with Navigation?
Use a navigate action, e.g., Navigation.createNavigateOnClickListener(actionId, bundle), or navigate via a NavController.
What is the difference between add() and replace() in FragmentTransaction?
add() inserts a fragment without removing existing ones; replace() removes the existing fragment(s) in the container and adds the new one.
In a two-pane UI, how do you decide between updating detail vs starting a new activity?
Use a boolean isDualPane; if true, update DetailFragment; if false, start DetailActivity.
How do you implement dynamic fragments with a FragmentContainerView?
Use FragmentContainerView as the container and perform fragment transactions (add/replace) at runtime to display fragments.
What is the purpose of the layout-sw600dp resource directory?
To provide tablet-optimized layouts for devices with sw600dp or larger.
How can you implement a color-changing feature with fragments?
Create a ColorFragment with OnClickListeners on color buttons that modify a TextView's color in the fragment.
What does DetailFragment.setStarSignData(starSignId) do?
It updates the DetailFragment's UI with the corresponding star sign data (name, symbol, date range) based on the ID.
How do you pass data to DetailFragment using a factory method?
Use DetailFragment.newInstance(starSignId) to create the fragment with STARSIGNID in arguments.
What is a major advantage of Jetpack Navigation over manual fragment management?
It reduces boilerplate, handles back navigation, and centralizes navigation in a graph.
Where do you place a navigation graph file and what is it called typically?
In res/navigation/nav_graph.xml; NavHostFragment uses it to manage destinations.
How do you adapt static dual-pane layouts for phones vs tablets?
Provide separate layouts in res/layout and res/layout-sw600dp to present different fragment arrangements and space allocations.
What is a Fragment lifecycle order for initial display (from creation to display)?
onAttach, onCreate, onCreateView, onViewCreated, onActivityCreated, onStart, onResume.
What is the role of a ListFragment in the dual-pane navigation example?
It displays a list of items and notifies when an item is selected to update detail or start a new screen.
How do you implement a DetailActivity for non-dual-pane devices?
Launch a separate activity (DetailActivity) to show details when isDualPane is false.
What is a NavHostFragment used for in Jetpack Navigation?
A fragment that acts as a host for navigation, swapping between destinations defined in nav_graph.
Why is a factory method used with DetailFragment in dynamic fragments?
To pass arguments (like STARSIGNID) when creating the fragment instance.