In my MainActivity.kt
file, I can access supportActionBar.title
and set the title.
In TitleScreenFragment.kt
, I cannot access:
activity.supportActionBar.title
and when I try to just use activity.actionBar.title
, that value is null.
How do I access activity.supportActionBar.title
from within my fragment code?
MainActivity.kt:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this,
R.layout.activity_main)
val navController = this.findNavController(R.id.nav_host_fragment)
NavigationUI.setupActionBarWithNavController(this, navController)
//THIS WORKS HOW DO I ACCESS THIS FUNCTIONALITY FROM FRAGMENT CODE???
supportActionBar?.title = "Ingredient Display"
}
override fun onSupportNavigateUp(): Boolean {
val navController = this.findNavController(R.id.nav_host_fragment)
return navController.navigateUp()
}
}
TitleScreenFragment.kt
class TitleScreenFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Setup the binding object with the layout and inflate it
val binding = DataBindingUtil.inflate<FragTitleBinding>(inflater, R.layout.frag_title, container, false)
// Set navigation action as an onclicklistener for button
binding.btnBeginInput.setOnClickListener{
view?.findNavController()?.navigate(R.id.action_titleScreenFragment_to_ingredientInputFragment)
}
//THIS IS NULL
println(activity?.actionBar?.title)
// return the inflated object in the binding
return binding.root
}
}
You can use something like:
(activity as AppCompatActivity).supportActionBar?.title = "Ingredient Display"
in the onViewCreated
method (not in onCreateView
).