简体   繁体   中英

Android: when to set Toolbar title form Fragment?

I have a ViewPager that contains some Fragments.

I need to update the title on the Activity Toolbar when the fragment changes.

In which lifecycle method of the Fragment should I call the Activity to set the new title?

It would be nice to use setUserVisibleHint(true) , but sometimes it gets called when the Activity is not actually ready (Toolbar is null).

And additionally I need to manage the config change and need to makes sure that the title in the Toolbar is the one of the Fragment currently displayed.

Thanks

I think the better option is do that in onPageSelected()

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

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