Upgrade to Pro — share decks privately, control downloads, hide ads and more …

NSC AD 340 5210 - Week 5

Nate Ebel
May 10, 2020
280

NSC AD 340 5210 - Week 5

Nate Ebel

May 10, 2020
Tweet

Transcript

  1. Week 5
    Lecture
    Fragments

    View Slide

  2. What are we building
    this week?
    Building composable UI using
    Fragments
    02
    Project Demo Fragments
    01

    View Slide

  3. Project Demo
    What are we building
    this week?

    View Slide

  4. Week 5 Project
    Updates
    ● Create a LocationEntryFragment
    ● Create a CurrentForecastFragment
    ● Create an AppNavigator interface to navigate between fragments
    ● Add a FloatingActionButton to show LocationEntryFragment after
    forecast data is displayed

    View Slide

  5. Fragments
    Building composable UI
    using Fragments

    View Slide

  6. How to reuse pieces
    of UI?

    View Slide

  7. Fragments
    Reusable portions of UI that can be
    composed together within a single Activity

    View Slide

  8. Fragment
    Has its own layout and
    lifecycle. Can be defined in
    XML or added/removed
    programmatically
    FragmentManager
    Manages what Fragments
    are currently showing on
    the screen. Use the
    FragmentManager to
    add/remove a Fragment.
    FragmentTransaction
    Represents the addition or
    removal of a single
    Fragment from its
    container.
    Fragments

    View Slide

  9. Fragment 1
    Fragment 2 Fragment 3
    Composable UI
    Add & remove multiple
    fragments in a single screen

    View Slide

  10. Adding a Fragment Using XML
    android:id="@+id/locationEntryFragment"
    android:name="com.goobar.io.ad340.location.LocationEntryFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
    activity_main.xml

    View Slide

  11. Adding a Fragment Programmatically
    supportFragmentManager
    .beginTransaction()
    .add(R.id.root, Fragment())
    .commit()
    MainActivity

    View Slide

  12. Fragment Lifecycle
    Fragment lifecycle is very similar to Activity
    lifecycle

    View Slide

  13. Fragment Lifecycle
    onAttach()
    Added to Activity
    onStart()
    Fragment is visible on screen
    onStop()
    Fragment no longer visible
    onCreateView()
    Fragment layout is inflated
    and returned
    onPause()
    Fragment has been removed
    onDestroyView()
    Fragment view is destroyed
    onResume()
    Fragment is active on screen
    onDetach()
    Fragment is removed from
    Activity

    View Slide

  14. Check Canvas For
    Additional Resources
    & Assignments

    View Slide