0

I have seen different coding styles when it comes to implementing AsyncTask on Android for lightweight background operations. Though I have used static nested class implementation by far, I wanted to understand when and why are these implementations for AsyncTask preferred?

  1. Static nested class
  2. Non-static inner class
  3. Separate public class.

I believe the inner classes helps to tightly bound the class to a particular activity/fragment (encapsulation) and also code organization. So, what decides static vs non-static?

With separate classes, though it might be reusable, there's a lot of boiler plate code with use of interfaces to communicate and update the UI elements of fragment/activities. Also, how are each implementations susceptible to memory leaks - for instance when the screen rotates?

Possible duplicate : What's the correct way to implement AsyncTask? static or non static nested class?

Community
  • 1
  • 1
droidx
  • 2,172
  • 19
  • 26
  • *separate private classes* are not possible and all the top-level public classes are, by design, static. – Blackbelt Jun 21 '15 at 13:47
  • 2
    It depends on what the `AsyncTask` is an inner class of. If the `AsyncTask` is inside an `Activity`, use a `static` inner class. If you use a regular nested class, and a configuration change occurs while the `AsyncTask` is running, now you're in trouble, as the `AsyncTask` is (temporarily) leaking the old `Activity` instance, and if the `AsyncTask` is planning on talking to the `Activity`, it will be talking to the wrong one after a configuration change. Using a `static` inner class avoids the leakage. – CommonsWare Jun 21 '15 at 13:52
  • If the `AsyncTask` is inside a retained `Fragment`, a regular nested class is fine. Basically, you are saying that the life of the `AsyncTask` is the same as the life of the `Fragment`. Just be sure to `cancel()` the `AsyncTask` if the fragment is destroyed. If I am going to use an `AsyncTask`, this is my preferred approach, as the task can talk to the fragment (though not the activity) without worrying about configuration changes. If the `AsyncTask` is inside something else, whether it should be `static` or not would depend on what the "something else" is. – CommonsWare Jun 21 '15 at 13:52
  • 1
    I think you mean static nested class as Java has no concept of static inner class. – akhil_mittal Jun 21 '15 at 13:53
  • @Blackbelt ahh.yes - separate private classes not possible. About the top-level classes by design being static - could you please elaborate on it wrt to the question? – droidx Jun 21 '15 at 13:54
  • @akhil_mittal yes..thanks for the correction! :) – droidx Jun 21 '15 at 13:58
  • @droidx And non-static nested class is known as inner class :). So again no need for writing `non-static inner class`. – akhil_mittal Jun 21 '15 at 14:02
  • @CommonsWare I liked the suggestion of using a retained fragment. Also, a good pointer there about cancelling the async task on fragment destroy. However, I don't see a good benefit in having separate class files for AsyncTask. – droidx Jun 21 '15 at 14:13

0 Answers0