For the More Curious: Accessing the AppCompat App Bar

As you saw in this chapter, you can change the contents of the app bar by adding menu items. You can also change other attributes of the app bar at runtime, such as the title it displays.

To access the AppCompat app bar, you reference your AppCompatActivity’s supportFragmentManager property. From CrimeFragment, it would look something like this:

    val appCompatActivity = activity as AppCompatActivity
    val appBar = appCompatActivity.supportActionBar as Toolbar

The activity that is hosting the fragment is cast to an AppCompatActivity. Recall that because CriminalIntent uses the AppCompat library, you made your MainActivity a subclass of AppCompatActivity, which allows you to access ...

Get Android Programming: The Big Nerd Ranch Guide, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.