java - Sending data from FragmentController's onActivityResult to Fragment -
hello have problem sending string fragmentcontroller fragment because onactivityresult in fragmentcontroller done later creating of fragments theirs ui's. want pass string fragmentcontroller , show textview in fragment.
my fragmentcontroller:
public class fragmentcontroller extends appcompatactivity implements home.onfragmentinteractionlistener, certificate.onfragmentinteractionlistener, challenges.onfragmentinteractionlistener, googleapiclient.onconnectionfailedlistener{ private googleapiclient googleapiclient; private static final int req_code = 9001; private string name = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_fragment_controller); googlesigninoptions signinoptions = new googlesigninoptions.builder(googlesigninoptions.default_sign_in).requestemail().build(); googleapiclient = new googleapiclient.builder(this).enableautomanage(this,this).addapi(auth.google_sign_in_api,signinoptions).build(); signin(); sectionspageradapter msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); viewpager mviewpager = (viewpager) findviewbyid(r.id.container); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); return id == r.id.action_settings || super.onoptionsitemselected(item); } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { } private void signin(){ intent intent = auth.googlesigninapi.getsigninintent(googleapiclient); startactivityforresult(intent,req_code); } private void handleresult(googlesigninresult result){ if(result.issuccess()){ googlesigninaccount account = result.getsigninaccount(); assert account != null; name = account.getdisplayname(); system.out.println("6"); } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { system.out.println("5"); super.onactivityresult(requestcode, resultcode, data); if(requestcode == req_code){ googlesigninresult result = auth.googlesigninapi.getsigninresultfromintent(data); handleresult(result); } } public string getname() {return name;} }
my sectionspageradapter:
class sectionspageradapter extends fragmentpageradapter { sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch (position) { case 0: return fragment1.newinstance(); case 1: return fragment2.newinstance(); case 2: return fragment3.newinstance(); default: return null; } } @override public int getcount() {return 3;} @override public charsequence getpagetitle(int position) { switch (position) { case 0: return "fragment1"; case 1: return "fragment2"; case 2: return "fragment3"; default: return null; } } }
and fragment want view string fragmentcontroller (username):
public class fragment2 extends fragment { public fragment2() {} public static fragment2 newinstance() {return new fragment2();} @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { fragmentcontroller fragmentcontroller = (fragmentcontroller) getactivity(); view view = inflater.inflate(r.layout.fragment_fragment2, container, false); textview user = view.findviewbyid(r.id.certificateuser); user.settext(fragmentcontroller.getname()); return view; } @override public void onattach(context context) {super.onattach(context);} @override public void ondetach() {super.ondetach();} public interface onfragmentinteractionlistener {} }
if want print string everywhere null if print directly in handleresult there correct username. debugging , find out onactivityresult done last , issue fragment has null string because created , string passed before inicialization.
Comments
Post a Comment