java - Return to Fragment A from Fragment B with a value -
java - Return to Fragment A from Fragment B with a value -
i know there have been many threads this, cant seem understand how nail it.. seek after button click in fragment b want homecoming fragment id value.. in backstack go fragment lead fragment b , when pressing button want go fragment value show. onclick listener in fragment b:
//fragment b called fragment addbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { if(value.gettext().tostring().trim().length() != 0) { string order = value.gettext().tostring(); //the value want in fragment b //return fragment } }
in mainactivity tread fragment this:
//fragmentmanager initialized in oncreate() ... private void selectitem(int position) { fragment fragment = null; string ftag = null; switch (position) { case 0: fragment = new fragmenta(); ftag = "fragmenta"; break; case 1: fragment = new fragmentc(); ftag = "fragmentc"; break; case 2: fragment = new fragmentd(); ftag = "fragmentd"; break; default: break; } if (fragment != null) { if(replacefragments(fragment, ftag)) { mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(mnavigationdraweritemtitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } } else { log.e(logtag, "error in creating fragment"); } } public boolean replacefragments(fragment fragment, string ftag) { fragmentmanager.begintransaction().replace(r.id.content_frame, fragment, ftag).addtobackstack(ftag).commit(); homecoming true; } @override public void onbackpressed() { int count = fragmentmanager.getbackstackentrycount(); if (count == 0) { super.onbackpressed(); } else { getsupportfragmentmanager().popbackstack(); //todo: add together app title on count == 1 } }
your onclick method should this:
@override public void onclick(view view) { if(value.gettext().tostring().trim().length() != 0) { string order = value.gettext().tostring(); //the value want in fragment b //return fragment fragmenta fragment = new fragmenta(); bundle args=new bundle(); args.putstring(order); fragment.setarguments(args); getfragmentmanager().begintransaction().replace((r.id.content_frame,fragment).commit(); }
and fragmenta can recover value with:
getarguments();
java android android-fragments
Comments
Post a Comment