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:
actionBaris deprecated and replaced withsupportActionBar.- Using
activitywithin a fragment returnsFragmentActivitywhich doesn't directly referencesupportActionBarobject; 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 = ...
