I want to change the text of the action bar in a fragment dynamically when that fragment is created,but activity?.actionBar?.title = movie.title
is not working.
I tried this too
activity?.actionBar?.setDisplayShowTitleEnabled(true)
activity?.actionBar?.title = movie.title
CodePudding user response:
actionBar
is deprecated and replaced withsupportActionBar
.- Using
activity
within a fragment returnsFragmentActivity
which doesn't directly referencesupportActionBar
object; instead you need to cast that toAppCompatActivity
) or to your custom name of the activity that hosts this fragment.
To fix this:
(requireActivity() as AppCompatActivity).supportActionBar?.setDisplayShowTitleEnabled(true)
(requireActivity() as AppCompatActivity).supportActionBar?.title = ...