java - Android external Async Task with callback, do i need to dispose the callback? -


got small issue , advice on it.

this async task

public class dummytask extends asynctask<void, void, void> {      private ondummytaskcomplete mondummytaskcomplete;      public dummytask(ondummytaskcomplete listener) {         mondummytaskcomplete = listener;     }      @override     protected void doinbackground(void... params) {         try {             thread.sleep(10000);         } catch (interruptedexception e) {             e.printstacktrace();         }         return null;     }      @override     protected void onpostexecute(void avoid) {         mondummytaskcomplete.ondummytaskcomplete();     } } 

this callback

public interface ondummytaskcomplete {     void ondummytaskcomplete(); } 

this activity implements callback starts async task.

public class mainactivity extends actionbaractivity implements ondummytaskcomplete {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          dummytask dummytask = new dummytask(this);         dummytask.execute();     }  

do need dispose of callback avoid memory leak? following code required?

@override protected void onpostexecute(void avoid) {     mondummytaskcomplete.ondummytaskcomplete();     mondummytaskcomplete = null; } 

do need dispose of callback avoid memory leak?

no. shortly after onpostexecute() run, asynctask instance lose scope, along reference mondummytaskcomplete. sure not reference mondummytaskcomplete (or else may contain reference mainactivity) within doinbackground; otherwise, open context leak. see this related issue details.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

Bubble Sort Manually a Linked List in Java -

asp.net mvc - SSO between MVCForum and Umbraco7 -