android - Add fragment and update actionbar title -
i know question refreshing actionbar title has been answered. problem quiet different.
i use fragments add method , not replace method reasons. previous fragment not destroy , when back, previous fragment aren't not recreating.
this configuration :
fragment title "fraga" > fragment b title "fragb"
when go fragment fragment b actionbar title should "fraga" stay "fragb". problem add method fragment not recreating , didn't find event refresh it.
the simple solution found :
1- fragb.onresume : save previous action bar title
2- fragb.ondestroyview : restore previous actionbar title
with solution, result ok, found solution not clean. there better way refresh actionbartitle using add method fragments ?
you can override onbackpressed
of activity , each time pressed name of fragment backstack know fragment current at.
sample:
@override public void onbackpressed() { super.onbackpressed(); int framentcount = this.getfragmentmanager().getbackstackentrycount(); if(framentcount != 0) { fragmentmanager.backstackentry backentry=getfragmentmanager().getbackstackentryat(framentcount-1); string str=backentry.getname(); //the tag of fragment if(str.equals("fraga")) //set actionbar title fraga else if(str.equals("fragb")) //set actionbar title fragb } fraga myfraga = (fraga)getfragmentmanager().findfragmentbytag("my_fragmenta_a_tag"); if (myfraga.isvisible()) { //action bar.title="title first fragment" } }
now know fragment need put tag fragment when add / replace backstack. make sure call addtobackstack
put fragments backstack.
fragmenttransaction.add(int containerviewid, fragment fragment, string tag) fragmenttransaction.replace(int containerviewid, fragment fragment, string tag)
Comments
Post a Comment