android - Correctly handling fragment interactions from an Async-Task -



android - Correctly handling fragment interactions from an Async-Task -

i trying refactor fragment-heavy code safer async tasks had noticed occasional crashes. improve educated after searches i've noticed main problem accessing activity or fragment variables within async task itself.

i separating each async task single classes, implement interface callbacks fragment or activity called them. example:

public class loginfragment extends fragment implements requestforgotpassword { ... //on forgot password click new asyncforgotpassword(loginfragment.this, memail).execute(); ... @override public void onforgotpasswordresult(result result) { if(isadded()) { if (result == result.success) toast.maketext(getactivity(), "an email has been sent business relationship assist in recovering password", toast.length_long).show(); else toast.maketext(getactivity(), "we don't have record of registered email, sorry! please seek again", toast.length_long).show(); } } ... }

ontaskcompleted.java

public interface ontaskcompleted { enum result{success, failure}; }

requestforgotpassword.java

public interface requestforgotpassword extends ontaskcompleted { void onforgotpasswordresult(result result); }

asyncforgotpassword.java

public class asyncforgotpassword extends asynctask<void, void, jsonobject> { private requestforgotpassword mrequest; private string memail; public asyncforgotpassword(requestforgotpassword request, string email) { mrequest = request; memail = email; } @override protected jsonobject doinbackground(void... params) { serverfunctions serverfunctions = new serverfunctions(); homecoming serverfunctions.forgotpassword(memail); } @override protected void onpostexecute(jsonobject obj) { seek { jsonobject json1 = obj.getjsonobject("response"); if (json1.getstring("success").matches("1")) { mrequest.onforgotpasswordresult(ontaskcompleted.result.success); } else { mrequest.onforgotpasswordresult(ontaskcompleted.result.failure); } } grab (jsonexception e) { e.printstacktrace(); } } }

so think if async tasks written this. many feature progress dialogs , long running, or feature meaningful interactions calling fragment etc. , i've come across interesting discussions here, here, here , here have led me approach set-up.

my understanding of dianne's code in first link's reply (written way in 2010) ensures activity never null when accessed async task. app has couple of activities , many fragments can't see how approach work nicely today. have had problems swapping fragments async tasks etc. in past , want create sure won't more null context related problems.

so if check isadded() within fragment after async task has complete, getactivity() guaranteed non-null? should phone call executependingtransactions() just beforehand everytime create sure? overkill?

thankyou feedback!

from doc:

public final boolean isadded () homecoming true if fragment added activity.

so activity not null.

edit:

to accomplish long running task recommend utilize intentservice (if task complex local operations, otherwise asynctask ok), save info in db (or in other forms, illustration in sharedpreferences if little chunk of informations) , propagate logic using localbroadcast. manage check in oncreate() callback of fragment if detached when task expired, retrieve info persisted , manage them properly; after that, flush info stored previously.

android android-fragments android-asynctask android-context

Comments

Popular posts from this blog

php - Edges appear in image after resizing -

ios8 - iOS custom keyboard - preserve state between appearances -

Delphi change the assembly code of a running process -