android - FragmentStatePagerAdapter / Update Action Bar Title -


i have host activity viewpager+fragmentstatepageradapter let me swipe through set of fragments (different instances of same itemfragment class) , data populating each fragment fetched network.

now, while swipe want update action bar title property coming current fragment use onpagechangelistener/onpageselected work:

... @override public void onpageselected(int position) {     myfragment currentfragment = (myfragment) getsupportfragmentmanager().findfragmentbyid(r.id.view_pager);     getsupportactionbar().settitle(currentfragment.propertyiwanttouseastitle); } ... 

i have couple of doubts here:

  • am retrieving real current fragment? fragmentstatepageadapter not set tag each fragment don't know other way current fragment rather using viewgroup container
  • when onpageselected invoked, added fragment still fetching data need set action bar title causing npe.

what's best place keep logic of updating action bar title in scenario described above?

ps. have tried mixed approach adding myfragment code update ab when data need available. problem few fragments loaded ahead having action bar title changing fragments finish loading. result ab title random title coming fragment few pages ahead.

thanks in advance helping!

afaict, pageradapter should called setprimaryitem() when new page set "current". override method, chain superclass, cast object (first parameter setprimaryitem()) appropriate fragment class , it. "something" stashing in data member of pageradapter (e.g., currentpage) , adding getter able retrieve later.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -