android - Reload the Fragment is not Working -
i have following situation: navigationdrawer creates fragment handle user information. fragment on oncreateview() calls web service , return view. when web services finishes callsback fragment detach , attach itself. when fragment attached second time on oncreateview() should create view user information. in fact happening view not change.
what missing?
thank in advance,
fragment code:
private boolean misrefresh = false; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // gets application context mcontext = getactivity().getapplicationcontext(); // set environment use database mglobal = (global) getactivity().getapplicationcontext(); mdaofactory = mglobal.getdaofactory(); mfragment = this; mfragmentview = inflater.inflate(r.layout.fragment_user_info, container, false); buildview(); if (misrefresh) { return mfragmentview; }else { return null; } }
here buildview() code:
private void buildview() { metusername = (edittext) mfragmentview.findviewbyid(r.id.username); metuseremail = (edittext) mfragmentview.findviewbyid(r.id.useremail); metpassword = (edittext) mfragmentview.findviewbyid(r.id.password); metpasswordconfirmation = (edittext) mfragmentview.findviewbyid(r.id.passwordconfirmation); mibsendpassword = (imagebutton) mfragmentview.findviewbyid(r.id.sendpassword); macusercountry = (autocompletetextview) mfragmentview.findviewbyid(r.id.usercountry); metuserstate = (edittext) mfragmentview.findviewbyid(r.id.userstate); metusercity = (edittext) mfragmentview.findviewbyid(r.id.usercity); metuserphonenumbercountry = (edittext) mfragmentview.findviewbyid(r.id.userphonenumbercountry); metuserphonenumbercity = (edittext) mfragmentview.findviewbyid(r.id.userphonenumbercity); metuserphonenumber = (edittext) mfragmentview.findviewbyid(r.id.userphonenumber); mcbtcaccepted = (checkbox) mfragmentview.findviewbyid(r.id.cbtcaccepted); // user has not been created if (mglobal.getuseruid() == 0) { getuseremail(); getuserlocation(); } else { if (misrefresh) { formatview(); } else { getdataonserver(); } } }
here code web service called:
private void getdataonserver () { muserdao = mdaofactory.getuserdao(mcontext); final progressdialog progressdialog = progressdialog.show (getactivity(), getstring(r.string.msgconnectingserver), getstring(r.string.msggettingdata), true); //todo retirar o set abaixo mglobal.setuseruid(1); muserdao.getuserbyuid(mglobal.getuseruid(), new callback<user>() { @override public void success(user user, response response) { muser = user; misrefresh = true; refresh(); } @override public void failure(retrofiterror error) { muser = null; log.e(getclass().getname(), "retrofit error = " + error); refresh(); } private void refresh() { progressdialog.dismiss(); fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.detach(mfragment) .attach(mfragment) .commit(); } }); }
finally onstart()
@override public void onstart() { super.onstart(); if (misrefresh) { misrefresh = false; getfragmentmanager().begintransaction() .remove(mfragment) .add(r.id.content_frame, mfragment) .commit(); } }
instead of
fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.detach(mfragment).attach(mfragment).commit();
do this:
yourfragment mfragment = getsupportfragmentmanager().findfragmentbytag("your_fragment_tag"); //if using support library //or yourfragment mfragment = getfragmentmanager().findfragmentbytag("your_fragment_tag"); //if using support library fragmentmanager fm = getfragmentmanager(); fragmenttransaction ft = fm.begintransaction(); ft.detach(mfragment); ft.attach(mfragment).commit();
Comments
Post a Comment