[英]How do I update a toolbar title on fragment update
I am setting up an activity that hosts fragments so what i want is to update a toolbar title in my activity whenever a fragment transaction happens. 我正在设置一个托管片段的活动,因此,每当发生片段事务时,我想要的是在活动中更新工具栏标题。 Currently only the first fragment transactions upates the title but the following transaction are not able to do so despite that part of my code being reached can any one help?
目前,只有第一个片段事务会更新标题,但后续事务却无法这样做,尽管我的代码部分已经到达了,可以帮上忙吗?
so here is some code that I have tried. 所以这是我尝试过的一些代码。
from the activity, I set up a method called setUp toolbar like so 从活动中,我建立了一个名为setUp工具栏的方法,如下所示
private void setUp(String title) {
toolbar=binding.authtoolbar;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
}
which I called from the fragment like so. 我从片段中这样称呼它。
((MyActivtyClass)getActivity()).setUp(toolbar)
then I later changed to 然后我改成了
toolbar=binding.authtoolbar;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(title);
and in the fragment, I change it to this 在片段中,我将其更改为
((MyActivtyClass)getActivity()).getSupportActionBar().setTitle("myTitle");
but still nothing. 但还是一无所有。 This issue has taken up a lot of my time and I would appreciate if I could get some help on this
这个问题占用了我很多时间,如果能在这方面得到帮助,我将不胜感激
尝试这个
getActivity().setTitle("your title");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.