简体   繁体   中英

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

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM