1

I'm wondering where I should place my AsyncTask in my android project. As of right now I'm implementing an AsyncTask as a private class of my activity its running under. What I am going to do is in each activity that has a network call I will implement its own private class of AsyncTask. I have a few questions though

  1. In The preexecute method it says I can interact with the activity and place a spinner or progress bar. I do this by using My_Activity_Class_Name.this. So my question is does that line of code reference the activity the AsyncTask is called from? If so I believe that will be a static method. How do i actually pass in the instance of the class so I can interact with non static functions?

  2. I want to place all my Async code into one class for its respective needs. My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?

Community
  • 1
  • 1
Esko918
  • 1,397
  • 1
  • 12
  • 25

1 Answers1

1

You should make your inner private AsyncTask class - static. This is because otherwise it will have implicit reference to your Activity - this means that if your Activity will be recreated - ie. due to config change - then your AsyncTask will still hold your activity reference causing reference leak. This means you should pass reference to your activity to AsyncTask in ie. constructor of AsyncTask, and keep it in WeakReference (WeakReference/AsyncTask pattern in android).

So my question is does that line of code reference the activity the ASYNC Task is called from?

yes, but only if your AsyncTask class is non static

If so I beleve that will be a static method. How do i actually pass in the instance of the class so i can interact with non static functions?

its not a static method, with My_Activity_Class_Name.this you can access non static methods of your Activity class.

My quesiotn though is if i need to return a type back to the class that calls the Async method how can I return a value? Also is this the best practice?

You can call a method on your Activity class, there is nothing wrong with that. Remember that you cannot access UI widgets from non UI thread. So update your Activity widgets from onPostExecute which is called on UI thread.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • When i make the class static I get the error Error:(104, 34) error: non-static variable this cannot be referenced from a static context. Creating a weak reference will fix that correct? – Esko918 May 21 '15 at 19:56
  • @Esko918 yes, instead of making your AsyncTask static inside your Activity, make it an external class - here is an example: https://gist.github.com/rorist/459787 – marcinj May 21 '15 at 20:19
  • I think i did it right I placed a class variable in the Static Async method like this private WeakReference introduction_activityWeakReference; But how can i check the reference count of my variables/ – Esko918 May 21 '15 at 20:55
  • @Esko918 you check if your WeakReference is non null by calling get() on it, if its null then its null .... – marcinj May 21 '15 at 21:19
  • Ya i want to add the code here but i cant becasue its a comment. How can i post the code i want? Whats the correct code format i tried
    
    int i = 0;
    
    See how its not working for me :/
    – Esko918 May 22 '15 at 15:01