android - Why isn't one of my tabs acting like the others? -
i'm still working on passing data between each fragment in tab navigation app. using callbacks set , value, didn't work properly. switched current method of calling methods in activity set , values, that's acting strange well.
when run app first fragment (first tab) gets value, , able change it. when swipe second tab, value not should be. when swipe third tab, value correct. value persistent on first , third tab (fragment), second tab doesn't want cooperate.
example:
- start - persistent_value = 0
- tab1 displayed - value shown 0
- change value on tab1 - persistent_value = 9
- swipe tab2 - value shown 0
- swipe tab3 - value shown 9
- change value on tab3 - persistent_value = 34
- swipe tab2 - value shown 0
- swipe tab1 - value shown 34
i'm not sure why second tab not updating, while both first , third are. doing wrong, or there i'm not aware of here?
here's example code i'm using try , solve problem.
public class mainactivity extends activity implements actionbar.tablistener { sectionspageradapter msectionspageradapter; viewpager mviewpager; int persistent_value = 0; public void setpersistentvalue(int value) { this. persistent_value = value; } public int getpersistentvalue() { return this.persistent_value; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); final actionbar actionbar = getactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); msectionspageradapter = new sectionspageradapter(getfragmentmanager(), this); mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); mviewpager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); (int = 0; < msectionspageradapter.getcount(); i++) { actionbar.addtab( actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { mviewpager.setcurrentitem(tab.getposition()); } public class sectionspageradapter extends fragmentpageradapter { private context context; public sectionspageradapter(fragmentmanager fm) { super(fm); } public sectionspageradapter(fragmentmanager fm, context context) { super(fm); this.context = context; } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getresources().getstring(r.string.tab_one_title); case 1: return getresources().getstring(r.string.tab_two_title); case 2: return getresources().getstring(r.string.tab_three_title); } return null; } } public class mainfragment extends fragment { private static final string arg_section_type = "section type"; int persistent_value; public mainfragment(){} public mainfragment(int sectionnumber) { bundle args = new bundle(); args.putint(arg_section_type, sectionnumber); setarguments(args); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); persistent_value = ((mainactivity)getactivity()).getpersistentvalue; textview textview = (textview) rootview.findviewbyid(r.id.the_value); textview.settext(integer.tostring(this.persistent_value)); return rootview; } @override public void onpause() { super.onpause(); ((mainactivity)getactivity()).setpersistentvalue(this.persistent_value); } @override public void onresume() { super.onresume(); persistent_value = ((mainactivity)getactivity()).getpersistentvalue; view rootview = getview(); textview textview = (textview) rootview.findviewbyid(r.id.the_value); textview.settext(integer.tostring(this.persistent_value)); } } }
it seems in wrong way.
if need pass data between fragments, need use interface , not going pass data directly 1 fragment another, instead going pass activity first receiver fragment
please refer link fragment - fragment communication
http://developer.android.com/training/basics/fragments/communicating.html
Comments
Post a Comment