Android Volley onResponse & recreating an Activity -
when recreate activity (changing device orientation) , waiting volley request response, code on "onresponse" has no effect since running on destroyed activity.
this basic case. make easy, use "mwaitingserverresponse" key info. works fine:
public void onloginbuttonclick(view view) { // store waiting server mwaitingserverresponse = true; sendloginrequest(); } public void sendloginrequest() { stringrequest loginrequest = new stringrequest( request.method.get, url, null, new response.listener<string>() { @override public void onresponse(string response) { // store not waiting server anymore mwaitingserverresponse = false; } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // store not waiting server anymore mwaitingserverresponse = false; } }); mvolley.addtorequestqueue(loginrequest); }
then value of "mwaitingserverresponse" remembered savedinstancestate.
so when onresponse run, changes value of "mwaitingserverresponse" on destroyed activity, not on 1 created after device orientation changed. how make onrespond act on new activity?
self answer other people similar issue:
now going try sending request fragment setretaininstance(true)
. way re-creating activity because of configuration change (such device orientation) wouldn't affect it.
to avoid leaking, think request should canceled on fragment ondestroy()
, , fragment must not contain reference object tied activity, such drawable, adapter, view or other object that's associated context.
Comments
Post a Comment