43

I'm currently developing some Android Apps in a team and we've used 2 different approaches during the last months (one that i personally prefere, and the other that the other developer prefers).

Although so far the results are the same, this got me wondering...should we:

  • use AsyncTasks as private classes inside the Activities that use them.
  • or use AsyncTasks as separate public classes that receive the context of the activity

Are any of the approachs recommended by Google?

What does your experience say about this (advantages, disadvantages, problems)?

neteinstein
  • 17,529
  • 11
  • 93
  • 123

2 Answers2

36

Inner classes are good for representing objects that are meant to be private or somehow intimately tied to the enclosing class. Occasionally there are technical reasons for using inner classes (e.g., simulating closures). They also cut down on namespace pollution.

One disadvantage of inner classes is that if they access private members (fields or functions) of the enclosing class, the compiler will generate accessor functions to those members. Language purists will argue whether this breaking of encapsulation is a Good Thing or a Bad Thing. The access functions add a bit of overhead to each access (which usually isn't a factor, but there it is). Another disadvantage is that it makes the source file more complex and therefore harder to manage. (I've occasionally been stung by editing a function in the inner class while thinking it was in the outer class, and vice versa.) Finally, inner classes tend not to be reusable, while separate classes can often be parameterized to have multiple uses.

These pros and cons are off the top of my head. I'm sure others will have additional thoughts.

UPDATE:

In this Google IO video the inner AsyncTask option is clearly marked as wrong option.

Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    After a while using them as separated public classes I see that it's much more useful. – neteinstein Dec 14 '11 at 19:28
  • 14
    @NeTeInStEiN - For an AsyncTask, an inner class (as compared to a nested static class) is always wrong for technical reasons: an Activity can be killed off (due to, say a configuration change) without stopping the AsyncTask. If the AsyncTask was an inner class, this would leak the old Activity because the AsyncTask would have a (hidden) reference to the outer class instance. A few orientation changes of the phone would then cause the process to die from memory exhaustion. – Ted Hopp Jan 27 '12 at 18:41
7

It doesn't matter, use what makes the most sense for your code. The important thing is to watch for an async task that is holding a reference on an activity after the activity has been destroyed, either implicitly as an inner class of the activity, or explicitly by being given the activity/context object.

hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 4
    See [this discussion](https://groups.google.com/forum/#!starred/android-developers/vwRrlc84gy0) for a solution to the (very common) problem of AsyncTask holding a reference to an activity. – Ted Hopp Nov 11 '11 at 16:52