WhoisAbel / AndroidBasicsInKotlin

In this project, you'll learn the basics of building Android apps with the Kotlin programming language. Along the way, you'll develop a collection of apps to start your journey as an Android developer.
0 stars 0 forks source link

Unit 3: Fragments and the Navigation Component #44

Open WhoisAbel opened 2 years ago

WhoisAbel commented 2 years ago

Fragment lifecycle

Like activities, fragments can be initialized and removed from memory, and throughout their existence, appear, disappear, and reappear onscreen. Also, just like activities, fragments have a lifecycle with several states, and provide several methods you can override to respond to transitions between them. The fragment lifecycle has five states, represented by the Lifecycle.State enum.

INITIALIZED: A new instance of the fragment has been instantiated. CREATED: The first fragment lifecycle methods are called. During this state, the view associated with the fragment is also created. STARTED: The fragment is visible onscreen but does not have "focus", meaning it can't respond to user input. RESUMED: The fragment is visible and has focus. DESTROYED: The fragment object has been de-instantiated.

image

WhoisAbel commented 2 years ago

The type should be FragmentLetterListBinding? and it should have an initial value of null. Why make it nullable? Because you can't inflate the layout until onCreateView() is called. There's a period of time in-between when the instance of LetterListFragment is created (when its lifecycle begins with onCreate()) and when this property is actually usable. Also keep in mind that fragments' views can be created and destroyed several times throughout the fragment's lifecycle. For this reason you also need to reset the value in another lifecycle method, onDestroyView().

WhoisAbel commented 2 years ago

Fragment:


   override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.layout_menu, menu)

        val layoutButton = menu.findItem(R.id.action_switch_layout)
        setIcon(layoutButton)
    }

Activity:


    /**
     * Initializes the [Menu] to be used with the current [Activity]
     */
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.layout_menu, menu)

        val layoutButton = menu?.findItem(R.id.action_switch_layout)
        // Calls code to set the icon based on the LinearLayoutManager of the RecyclerView
        setIcon(layoutButton)

        return true
    }
WhoisAbel commented 2 years ago
WhoisAbel commented 2 years ago

The Navigation component has three key parts which you'll use to implement navigation in the Words app:

WhoisAbel commented 2 years ago
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost="true"
WhoisAbel commented 2 years ago

Configure MainActivity

private lateinit var navController: NavController
override fun onSupportNavigateUp(): Boolean {
   return navController.navigateUp() || super.onSupportNavigateUp()
}